我正在尝试在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)
但是当我创造物体的时候
^{pr2}$我有以下错误:
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in column "id" violates not-null constraint
看起来我没有提供任何ID。那么,我如何使数据库自动生成或自己生成?在
您似乎正在使用this code。它缺少列的默认值。您正在模拟此SQL:
但你已经linked to the correct code。在
^{pr2}$或者,如果不想加载postgresuuid扩展,可以用Python创建UUID。在
您可以使用
uuid
模块并设置一个列默认值。例如:相关问题 更多 >
编程相关推荐