求SQLAlchemy模型字段全部可用选项及默认行为列表(原Django开发者)
Hey there! Since you’re coming from Django and diving into Flask with SQLAlchemy, I totally get wanting that clear, Django-style breakdown of field options. Let’s walk through the key ones you’ll use regularly, along with their defaults and behaviors:
Core Common Options
These apply to nearly all field types:
nullable: Controls whether the field can storeNULLvalues in the database.- Default:
True - Example: Your
email = db.Column(db.String(128), nullable=False)makes this field required at the database level.
- Default:
unique: Ensures every value in the field is distinct across the entire table.- Default:
False - Example: Pairing this with
nullable=Falsefor emails prevents duplicate user accounts with the same address.
- Default:
default: Sets a fallback value for the field when creating a new record.- Default:
None - Can be a static value (e.g.,
default="guest"), a database function (likedb.func.current_timestamp()for timestamps), or any callable that returns a valid value.
- Default:
onupdate: Defines a value to automatically set whenever the record is updated.- Default:
None - Example: Your
date_modifiedfield uses this to refresh the timestamp on every edit.
- Default:
primary_key: Marks the field as the unique identifier for records in the table.- Default:
False - You can set this on multiple fields to create a composite primary key.
- Default:
index: Creates a database index for the field to speed up filter/sort queries.- Default:
False - Great for fields you’ll frequently use in
WHEREclauses (likeemailfor user lookups).
- Default:
comment: Adds a human-readable description to the database column for documentation purposes.- Default:
None - Example:
comment="User's primary contact email"
- Default:
Type-Specific Options
Some field types have extra options tailored to their data type:
- For
Stringfields:length: Mandatory for most databases, defines the maximum character count (you used this withString(128)).
- For
DateTimefields:timezone: If set toTrue, stores timezone-aware datetime values instead of naive ones.- Default:
False
- Default:
- For
Integerfields:autoincrement: Enables auto-incrementing numeric values. Defaults toTrueif the field is a primary key, otherwiseFalse.
- For
ForeignKeyfields:ondelete: Specifies behavior when the referenced record is deleted (e.g.,ondelete="CASCADE"deletes related records,ondelete="SET NULL"sets the foreign key toNULLifnullable=True).onupdate: Defines behavior when the referenced record’s primary key is updated.
Default Behaviors Recap
A quick cheat sheet for what’s enabled by default:
- Almost all fields allow
NULLvalues unless you setnullable=False. - Fields are not unique by default.
- No default or auto-update values are set unless you explicitly define them.
- Primary keys auto-increment only if you use
db.Integer(primary_key=True). - No database indexes are created automatically—you have to set
index=Truemanually.
Quick Django-to-SQLAlchemy Mapping
Since you’re familiar with Django, here’s a quick translation to help you transition:
- Django’s
blank(form validation) doesn’t have a direct SQLAlchemy equivalent, butnullablematches Django’snull(database-level nullability). uniqueworks exactly the same in both frameworks.defaultsupports static values and callables just like in Django.- Django’s
auto_now_add≈ SQLAlchemy’sdefault=db.func.current_timestamp(). - Django’s
auto_now≈ SQLAlchemy’sonupdate=db.func.current_timestamp().
That covers the main options you’ll encounter day-to-day. If you hit edge cases or need deeper details on a specific field type, you can dive into the SQLAlchemy core docs—but this should give you that concise, Django-style reference you were looking for!
内容的提问来源于stack exchange,提问作者Kishor Pawar

