我正在尝试在Postgres数据库中创建对象。
我正在使用这种方法https://websauna.org/docs/narrative/modelling/models.html#uuid-primary-keys。
class Role(Base):
__tablename__ = 'role'
# Pass `binary=False` to fallback to CHAR instead of BINARY
id = sa.Column(UUIDType(binary=False), primary_key=True)
但是当我创建对象时
user_role = Role(name='User')
db.session.add(user_role)
db.session.commit()
我遇到了以下错误:
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in column "id" violates not-null constraint
看起来我没有提供任何ID。那么,我该如何让数据库自动生成ID或者自己生成ID呢?