0%

SQLAlchemy default 不更新

错误示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class UserModel(db.Model):


__tablename__ = "user"

#
id = Column(db.BIGINT, primary_key=True, unique=True, autoincrement=True)

uid = Column(db.String(36), unique=True, default=generate_uuid())
username = Column(db.String(32), unique=True)
password = Column(db.String(60))
category = Column(db.String(32), default=Constants.DB_UserModel.Category.USER.value)
mobile = Column(db.String(32), unique=True)
email = Column(db.String(32), unique=True)
register_date = Column(db.DATETIME, default=datetime.now())

1
2
def generate_uuid():
return str(uuid.uuid4())

正确示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class UserModel(db.Model):


__tablename__ = "user"

#
id = Column(db.BIGINT, primary_key=True, unique=True, autoincrement=True)

uid = Column(db.String(36), unique=True, default=generate_uuid)
username = Column(db.String(32), unique=True)
password = Column(db.String(60))
category = Column(db.String(32), default=Constants.DB_UserModel.Category.USER.value)
mobile = Column(db.String(32), unique=True)
email = Column(db.String(32), unique=True)
register_date = Column(db.DATETIME, default=datetime.now)

default 这个参数后可以指定一个函数

例如 default=generate_uuid, 如果 default=generate_uuid() 则设定为该函数的一个返回值, 即这样是设置了一个常量