gpt4 book ai didi

python - 为什么我收到 SQLAlchemy 错误 "__table_args__ value must be a tuple, dict, or None"

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:02 33 4
gpt4 key购买 nike

我有以下 SQLAlchemy 模型。已成功迁移至数据库:

class MyClassA(db.Model, Timestamp):
a_id = db.Column(db.Integer, nullable=False, primary_key=True)
b_id = db.Column(db.Integer, db.ForeignKey(C.c_id), nullable=False)
d = db.Column(db.String(1024))
e_id = db.Column(db.Integer,
db.ForeignKey(e.e_id))

现在我想在第二个和第四个字段中添加唯一性约束。所以我将以下行添加到模型中:

     __table_args__ = db.UniqueConstraint('b_id', 'e_id', name='unique_constraint_bid_eid')

但是现在当我尝试迁移它时,出现以下错误:

sqlalchemy.exc.ArgumentError: __table_args__ value must be a tuple, dict, or None

为什么会出现此错误?我该如何解决?我尝试将等式的右边放在括号中,但这并没有解决问题。

最佳答案

table_args 应该是元组、字典或 None,如错误代码所示。如果你把它变成一个元组,那么你必须把你的值放在括号里,最后还有一个逗号:

尝试:

     __table_args__ = (db.UniqueConstraint('b_id', 'e_id', name='unique_constraint_bid_eid'), )

关于python - 为什么我收到 SQLAlchemy 错误 "__table_args__ value must be a tuple, dict, or None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521770/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com