本文介绍了SQLAlchemy和连接,我们没有外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在MySQL中假设如下: pre $ $ codeREATE TABLE用户$ b $ id整数auto_increment主键 username int varchar(30), active enum('N','Y'), created_on int(11), updated_on int(11), points int 10), //其他字段); CREATE TABLE注释( id整数auto_increment主键, user_id整数, forum_id整数,整数, //其他字段); 请注意,没有正式的外键约束被添加到表中。这是我继承的东西,不能改变我们目前的设置。 (我们正在检修整个系统,但同时我必须处理我所得到的) 我无法绕过SQLalchemy如果没有在表格之间建立正式的外键,就加入。 实际上,我想要做一些类似的操作: SELECT b $ b u.username, c.forum_id, count(c.id) FROM users u JOIN注释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(评论,评论,primary_key = [comments.c.id], include_properties = ['active','user_id',' ('users.c.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 =在rdata中运行(查询)行: print row .. 。 $ p code $ sqlalchemy.exc.ArgumentError:找不到任何外键关系 '用户'和'评论'。 我不知道如何在没有外键的情况下解决这个问题。我还怎么定义这种关系?我认为这是mapper()调用的一部分: $ $ p $ $ $ c $ mapper(UserComments,j,properties = {'user_id':[ users.c.id, comments.c.user_id]}) ...但显然我误解了文档。 感谢您的帮助。 / div> 你有两个选择。您可以在 join 中传递连接条件,如下所示: j = join(users,comments,onclause = users.c.id == commends.c.user_id) 如果你用 orm.relationship 属性来定义它,关键字参数将会是 primaryjoin ,而不是 onclause 。 然而,我喜欢的方法只是 lie 。通知SQLAlchemy有一个外键,即使没有。 comments = Table('comments',元数据,列('id',Integer,primary_key = True),列('user_id',Integer,ForeignKey('users.id')), ... ) SQLAlchemy将继续进行,就像外键实际上存在一样,即使实际的数据库没有那个当然,如果隐含的foriegn键约束被违反( comments.user_id 没有相应的 users.id $ c>),但是你可能会遇到麻烦。 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_idWHERE u.id = 1234GROUP 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 relationshipsbetween '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. 解决方案 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. 这篇关于SQLAlchemy和连接,我们没有外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 12:59