When defining models in Flask-SQLAlchemy, each field is defined using db.Column
with specific attributes. These attributes control the behavior and constraints of the database columns.
Defines the primary key of the table.
id = db.Column(db.Integer, primary_key=True)
Ensures that all values in this column are unique.
email = db.Column(db.String(120), unique=True)
Specifies whether the column can have NULL
values. By default, columns are nullable.
username = db.Column(db.String(80), nullable=False)
Sets a default value for the column.
is_active = db.Column(db.Boolean, default=True)
Creates an index on the column to speed up queries.
created_at = db.Column(db.DateTime, index=True)
Defines the type of data that the column will store.
db.String
: Variable-length string.db.Integer
: Integer values.db.Float
: Floating-point numbers.db.Boolean
: Boolean values.db.DateTime
: Date and time values.db.Text
: Large text fields.
Establishes relationships between tables.
profile_id = db.Column(db.Integer, db.ForeignKey('profile.id'))
Specifies a default value at the database level rather than application level.
status = db.Column(db.String(50), server_default='active')
Adds constraints to validate data.
__table_args__ = (
db.CheckConstraint('age >= 18', name='check_age_positive'),
)
Defines relationships between tables.
profile = db.relationship('Profile', backref='user', lazy=True)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
created_at = db.Column(db.DateTime, index=True, default=datetime.utcnow)
is_active = db.Column(db.Boolean, default=True)
def __repr__(self):
return f'<User {self.username}>'
Understanding and using column attributes properly allows you to build robust and efficient databases. In the next section, we will cover relationships and more advanced features in Flask-SQLAlchemy.
If you don't want to limit the string length in SQLAlchemy, you have two options:
db.Text
stores long, unrestricted text (like passwords, descriptions, or large text data).
f_name = db.Column(db.String(30), nullable=False) # Limited to 30 chars
l_name = db.Column(db.String(30), nullable=False) # Limited to 30 chars
password = db.Column(db.Text, nullable=False) # No character limit
✅ db.Text
is best for long, unknown-length strings.
If you don't provide a limit, SQLAlchemy doesn't enforce one, but the database might have a default limit.
password = db.Column(db.String(), nullable=False) # No explicit limit
- Some databases (like SQLite) might still impose an internal limit.
- Using
db.Text
is safer for long values.
- Use
db.Text
if you truly need unlimited text (like large descriptions, comments, or passwords). - Use
db.String()
without a limit if you trust the database's default behavior.