類型名 | Python類型 | 數(shù)據(jù)庫類型 |
---|---|---|
Integer | int 整數(shù)(4字節(jié)) | INT |
SmallInteger | int取值范圍小的整數(shù)(2字節(jié)) | SMALLINT |
BigInteger | int或long | BIGINT |
Float | float | FLOAT |
Numeric | decimal.Decimal | DECIMAL |
String | str | VARCHAR |
Text | str | TEXT |
Boolean | bool | BOOLEAN |
Date | datetime.date | DATE |
Time | datetime.time | TIME |
DateTime | datetime.datetime | DATETIME |
Enum | str | ENUM |
LargeBinary | str | VARBINARY |
使用:
...
db = SQLAlchemy()
...
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
about_me = db.Column(db.String(140))
info = db.Column(db.Text)
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<User {}>'.format(self.username)