Performance: Column types and constraints
This affects the database query speed and the initial page load time when fetching data through Flask.
class User(db.Model): id = db.Column(db.Integer, primary_key=True) age = db.Column(db.Integer, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False)
class User(db.Model): id = db.Column(db.String(255), primary_key=True) age = db.Column(db.String(255)) email = db.Column(db.String(255), unique=False, nullable=True)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using large string types for numeric data | N/A | N/A | Increases server response time | [X] Bad |
| Using correct data types and constraints | N/A | N/A | Reduces server response time | [OK] Good |
db.String is designed for variable-length text, unlike db.Integer or db.Boolean.age in a Flask model?db.Integer is correct.nullable=False.age = db.Column(db.Integer, nullable=False) [OK]email = db.Column(db.String(120), unique=True, nullable=False)
unique=True constraint do?unique=True constraint ensures no two rows have the same value in this column.price = db.Column(db.String, nullable=False, unique='yes')
unique parameter expects a boolean value (True or False), not a string.unique='yes' is invalid and will cause an error.db.String(10) is better than db.Integer.unique=True to prevent duplicates and nullable=False to disallow empty values.phone = db.Column(db.String(10), unique=True, nullable=False) [OK]Pass the test and this pattern moves unknown