模型代码:
class Problems(db.Model):
__tablename__ = 'problems'
id = db.Column(db.String(45), primary_key=True)
......
def __init__(self):
self.id = str(uuid4())
用的是 Flask Admin 默认生成的模型视图,创建后提交报错
sqlalchemy.orm.exc.FlushError: Instance <Problems at 0x7e3b050> has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such as within a load() event.
- 这主键他默认也不显示,在构造函数里生成他也不调用。这个是 Flask Admin 需要设置的地方,还是因为我的代码有问题?
目前我只能加上默认自动生成,不知道有没有更好的办法,或者说问题的关键在哪里?
id = db.Column(db.String(45), primary_key=True, default=str(uuid4()))