从加入的 table 添加列
Adding columns from joined table
我正在使用 Python 2.7+flask-sqlalchemy。我定义了以下 models/table:
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: "Could not locate column in row for column 'details_usages.is_required'"
我做错了什么?
我找到了解决方案。正确的查询方式应该是:
models.Detail.query.join(details_usages).add_columns(details_usages.c.is_required).all()
我正在使用 Python 2.7+flask-sqlalchemy。我定义了以下 models/table:
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: "Could not locate column in row for column 'details_usages.is_required'"
我做错了什么?
我找到了解决方案。正确的查询方式应该是:
models.Detail.query.join(details_usages).add_columns(details_usages.c.is_required).all()