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

标签 python sqlalchemy flask-sqlalchemy sqlalchemy-migrate

我有以下 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/

相关文章:

python - 来自类别列中的类标记的多个数据帧的 pairplot 列

python - 将用户特定字段添加到 Django REST Framework 序列化程序

Python 3.4 Selenium 处理 chromedriver 异常

python - sqlalchemy 同时从 2 个表中计数

python - Flask-SQLAlchemy:照片列类型

python - pandas:根据条件分组并选择一行

python - SQLAlchemy:从连接多个表中获取单个对象

Python + sqlalchemy : how to process timedelta on database side?

sqlite - 两列使用 SqlAlchemy 加入同一个表

python - 使用 SQLAlchemy 获取按 ID 顺序放入表中的最后几条记录