SQLAlchemy 和联接,我们没有外键
在 MySQL 中假设如下:
CREATE TABLE users (
id integer auto_increment primary key,
username varchar(30),
active enum('N','Y'),
created_on int(11),
updated_on int(11),
points int(10),
// other fields
);
CREATE TABLE comments (
id integer auto_increment primary key,
user_id integer,
forum_id integer,
favorited integer,
// other fields
);
请注意,没有向表中添加正式的外键约束。这是我继承的,在我们当前的设置中无法更改。 (我们正在彻底检修整个系统,但与此同时,我必须处理所给的内容)
当表之间没有建立正式的外键时,我很难理解 SQLalchemy 的连接。
实际上,我想做类似的事情:
SELECT
u.username,
c.forum_id,
count(c.id)
FROM
users u
JOIN comments c ON u.id=c.user_id
WHERE
u.id = 1234
GROUP BY
u.username,
c.forum_id;
我拥有的代码包括如下内容:
mapper(Users, users, primary_key=[users.c.id],
include_properties=['user_id', 'username', 'active', 'created_on',
'updated_on', 'points'])
mapper(Comments, comments, primary_key=[comments.c.id],
include_properties=['active', 'user_id', 'favorited', 'forum_id'])
j = join(users, comments)
mapper(UserComments, j, properties={'user_id': [users.c.id,
comments.c.user_id]})
session = create_session()
query = session.query(UserComments).filter(users.cid == 1234)
rdata = run(query)
for row in rdata:
print row
...这当然会失败:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'users' and 'comments'.
当我们没有外键时,我不确定如何解决这个问题。我还能如何定义这种关系?我认为这是 mapper() 调用的一部分:
mapper(UserComments, j, properties={'user_id': [users.c.id,
comments.c.user_id]})
...但显然我误读了文档。
预先感谢您的任何帮助。
Assume the following in MySQL:
CREATE TABLE users (
id integer auto_increment primary key,
username varchar(30),
active enum('N','Y'),
created_on int(11),
updated_on int(11),
points int(10),
// other fields
);
CREATE TABLE comments (
id integer auto_increment primary key,
user_id integer,
forum_id integer,
favorited integer,
// other fields
);
Note that no formal foreign key constraints are added to the tables. This is something I've inherited and cannot change on our current setup. (We're overhauling the whole system, but in the meantime I have to work with what I've been given)
I'm having trouble wrapping my head around SQLalchemy's joins when there's no formal foreign key established between tables.
Effectively, I'd like to do something like:
SELECT
u.username,
c.forum_id,
count(c.id)
FROM
users u
JOIN comments c ON u.id=c.user_id
WHERE
u.id = 1234
GROUP BY
u.username,
c.forum_id;
Code I have includes things like the following:
mapper(Users, users, primary_key=[users.c.id],
include_properties=['user_id', 'username', 'active', 'created_on',
'updated_on', 'points'])
mapper(Comments, comments, primary_key=[comments.c.id],
include_properties=['active', 'user_id', 'favorited', 'forum_id'])
j = join(users, comments)
mapper(UserComments, j, properties={'user_id': [users.c.id,
comments.c.user_id]})
session = create_session()
query = session.query(UserComments).filter(users.cid == 1234)
rdata = run(query)
for row in rdata:
print row
... which of course fails with:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'users' and 'comments'.
I'm not sure how to work around this when we have no foreign keys. How else do I define the relationship? I thought it was part of the mapper() call:
mapper(UserComments, j, properties={'user_id': [users.c.id,
comments.c.user_id]})
... but apparently I've misread the documentation.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有两个选择。您可以在
join
中传递联接条件,如下所示:如果您根据
orm.relationship
属性定义此条件,则关键字参数将为primaryjoin< /code> 而不是
onclause
。然而,我更喜欢的方法是撒谎。通知 SQLAlchemy 有外键,即使没有。
SQLAlchemy 将继续处理,就好像外键实际上存在一样,即使实际的数据库没有外键。当然,如果违反隐含的外键约束(当没有相应的
users.id
时,comments.user_id
),您可能会遇到麻烦,但您可能会陷入困境反正麻烦。You have two options. You can pass the join condition in
join
like so:If you're defining this in terms of a
orm.relationship
property, the keyword parameter will beprimaryjoin
instead ofonclause
.However, the approach I Prefer is to just lie. Inform SQLAlchemy that there is a foreign key, even though there is not.
SQLAlchemy will the proceed as if the foreign key were in fact present, even though the actual database doesn't have that. Of course, you may run into trouble if the implied foriegn key constraint is violated (
comments.user_id
when there's no correspondingusers.id
), but you'd probably be in trouble anyway.