SQLAlchemy 和联接,我们没有外键

发布于 2024-11-15 14:36:18 字数 1670 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2024-11-22 14:36:18

你有两个选择。您可以在 join 中传递联接条件,如下所示:

j = join(users, comments, onclause=users.c.id == commends.c.user_id)

如果您根据 orm.relationship 属性定义此条件,则关键字参数将为 primaryjoin< /code> 而不是 onclause

然而,我更喜欢的方法是撒谎。通知 SQLAlchemy 有外键,即使没有。

comments = Table('comments', metadata,
    Column('id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey('users.id')),
    ...
)

SQLAlchemy 将继续处理,就好像外键实际上存在一样,即使实际的数据库没有外键。当然,如果违反隐含的外键约束(当没有相应的 users.id 时,comments.user_id),您可能会遇到麻烦,但您可能会陷入困境反正麻烦。

You have two options. You can pass the join condition in join like so:

j = join(users, comments, onclause=users.c.id == commends.c.user_id)

If you're defining this in terms of a orm.relationship property, the keyword parameter will be primaryjoin instead of onclause.

However, the approach I Prefer is to just lie. Inform SQLAlchemy that there is a foreign key, even though there is not. 

comments = Table('comments', metadata,
    Column('id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey('users.id')),
    ...
)

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 corresponding users.id), but you'd probably be in trouble anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文