问题出在主题标题中。所以这里有详细信息。
我正在使用Flask-SQLAlchemy和PostgreSQL,我的数据库架构是这样的:
class Settings(db.Model):
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(), unique=True)
lang = db.relationship('Lang', order_by='Lang.id')
def __init__(self, key, lang):
self.key = key
self.lang = lang
def __repr__(self):
return '<Settings %r, %r>' % (self.key, self.lang)
class Lang(db.Model):
__tablename__ = 'settings_lang'
id = db.Column(db.Integer, primary_key=True)
lang = db.Column(db.String(2))
value = db.Column(db.Text())
parent_id = db.Column(db.Integer, db.ForeignKey('settings.id'))
def __init__(self, lang, value):
self.lang = lang
self.value = value
def __repr__(self):
return '<Lang %r, %r>' % (self.lang, self.value)
我使用这样的查询:
for data in db.session.query(Settings).join(Lang).filter(Lang.lang.contains('en')).all():
print data
并希望它只返回&#39; en&#39;语言。但相反,我得到了所有的语言。
可能是什么问题以及如何处理它?</ p>
UPD:SQL输出
SELECT settings.id AS settings_id, settings.key AS settings_key
FROM settings JOIN settings_lang ON settings.id = settings_lang.parent_id
WHERE settings_lang.lang LIKE '%%' || %(lang_1)s || '%%'
{'lang_1': 'en'}
SELECT settings_lang.id AS settings_lang_id, settings_lang.lang AS settings_lang_lang,
settings_lang.value AS settings_lang_value,
settings_lang.parent_id AS settings_lang_parent_id
FROM settings_lang
WHERE %(param_1)s = settings_lang.parent_id ORDER BY settings_lang.id
{'param_1': 1}
来自settings_lang的示例行:
id parent_id lang value 1 1 "en" "Research" 2 1 "ru" "НПП"