gpt4 book ai didi

python - 从连接表中添加列

转载 作者:行者123 更新时间:2023-12-01 04:53:21 24 4
gpt4 key购买 nike

我正在使用Python 2.7+flask-sqlalchemy。我定义了以下模型/表:

class Detail(db.Model):
__tablename__ = 'details'
id = db.Column(db.Integer, primary_key=True)
....

class Usage(db.Model):
__tablename__ = 'details'
id = db.Column(db.Integer, primary_key=True)
details = db.relationship('Detail', secondary=details_usages, backref=db.backref('usages', lazy='dynamic'))
....

details_usages = db.Table('details_usages',
db.Column('detail_id', db.Integer, db.ForeignKey('details.id')),
db.Column('usage_id', db.Integer, db.ForeignKey('usages.id')),
db.Column('is_required', db.Integer)
)

我有以下查询:

models.Detail.query.join(details_usages).add_columns('details_usages.is_required').all()

我收到以下错误:

NoSuchColumnError:“无法在行中找到列“details_usages.is_required”的列”

我做错了什么?

最佳答案

我找到了解决方案。正确的查询方式应该是:

models.Detail.query.join(details_usages).add_columns(details_usages.c.is_required).all()

关于python - 从连接表中添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27990715/

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