简体   繁体   English

sqlalchemy通过到同一表的两个外键联接到一个表(模糊列错误)

[英]sqlalchemy join to a table via two foreign keys to that same table (ambiguous column error)

I am trying to do a join on a table that has two foriegn keys to the same table. 我正在尝试在具有到同一表的两个前键的表上进行联接。 Namely, sourceToOutputRelation points twice to Entries, as shown in the code. 即,如代码中所示,sourceToOutputRelation两次指向Entries。 Also, Entries have tags. 此外,条目具有标签。 I am trying to do a join so that I get every sourceToOutputRelation that has all the given tags (via Entries). 我正在尝试进行联接,以便获得具有所有给定标记(通过条目)的每个sourceToOutputRelation。 I am just trying to understand the join (the filtering works, I think). 我只是想了解联接(我认为过滤有效)。 Here is the code I have for the join and filter. 这是我用于联接和过滤的代码。 :

'''
  tags is a list of strings that are supposed to match the Tags.tag strings
'''
    from sqlalchemy.orm import aliased


    q = SourceToOutputRelation.query.\
         join(Entries.source_entries, Entries.output_entries).\
         join(original_tag_registration).\
         join(Tags).\
         filter(Tags.tag == tags[0]) 
    print(q.all())

Here are my model definitions : 这是我的模型定义:

class SourceToOutputRelation(alchemyDB.Model):

    __tablename__ =  'sourceToOutputRel'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    source_article = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('entries.id'))
    output_article = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('entries.id'))

class Entries(alchemyDB.Model):

    __tablename__ = 'entries'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)    
    tags = alchemyDB.relationship('Tags',
                              secondary = original_tag_registration,
                              backref = alchemyDB.backref('relevant_entries', lazy = 'dynamic'),
                              lazy = 'dynamic')    
    source_entries = alchemyDB.relationship('SourceToOutputRelation',
                                            primaryjoin="SourceToOutputRelation.output_article==Entries.id",
                                            foreign_keys = [SourceToOutputRelation.output_article],
                                            backref = alchemyDB.backref('output', lazy = 'joined'),
                                            lazy = 'dynamic',
                                            cascade = 'all, delete-orphan')
    output_entries = alchemyDB.relationship('SourceToOutputRelation',                                            
                                            primaryjoin="SourceToOutputRelation.source_article==Entries.id",
                                            foreign_keys = [SourceToOutputRelation.source_article],
                                            backref = alchemyDB.backref('source', lazy = 'joined'),
                                            lazy = 'dynamic',
                                            cascade = 'all, delete-orphan')



original_tag_registration = alchemyDB.Table('original_tag_registration',
    alchemyDB.Column('tag_id', alchemyDB.Integer, alchemyDB.ForeignKey('tagTable.id')),
    alchemyDB.Column('entry_id', alchemyDB.Integer, alchemyDB.ForeignKey('entries.id'))
    )


class Tags(alchemyDB.Model):
    '''
        a table to hold unique tags
    '''
    __tablename__ = 'tagTable'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    tag = alchemyDB.Column(alchemyDB.String(64), unique=True)
    entries_with_this_tag = alchemyDB.relationship('Entries',
                                                secondary = original_tag_registration,
                                                backref = alchemyDB.backref('tag', lazy = 'dynamic'),
                                                lazy = 'dynamic') 

I get this error : 我收到此错误:

OperationalError: (OperationalError) ambiguous column name: sourceToOutputRel.id u'SELECT "sourceToOutputRel".id AS "sourceToOutputRel_id", "sourceToOutputRel".source_article AS "sourceToOutputRel_source_article", "sourceToOutputRel".output_article AS "sourceToOutputRel_output_article", "sourceToOutputRel".needs_processing AS "sourceToOutputRel_needs_processing", "sourceToOutputRel".number_of_votes AS "sourceToOutputRel_number_of_votes", "sourceToOutputRel".date_related AS "sourceToOutputRel_date_related", "sourceToOutputRel".confirmed_relationship_type AS "sourceToOutputRel_confirmed_relationship_type", entries_1.id AS entries_1_id, entries_1.title AS entries_1_title, entries_1.text AS entries_1_text, entries_1.body_html AS entries_1_body_html, entries_1.user_id AS entries_1_user_id, entries_1.date_posted AS entries_1_date_posted, entries_2.id AS entries_2_id, entries_2.title AS entries_2_title, entries_2.text AS entries_2_text, entries_2.body_html AS entries_2_body_html, entries_2.us OperationalError:(OperationalError)模棱两可的列名:sourceToOutputRel.id u'SELECT“ sourceToOutputRel” .id AS“ sourceToOutputRel_id”,“ sourceToOutputRel” .source_article AS“ sourceToOutputRel_source_article”,“ sourceToOutputRel” .output_article AS“ sourceToOutputRel_output” ne AS“ sourceToOutputRel_needs_processing”,“ sourceToOutputRel” .number_of_votes AS“ sourceToOutputRel_number_of_votes”,“ sourceToOutputRel” .date_related AS“ sourceToOutputRel_date_related”,“ sourceToOutputRel” .confirmed_relationship_title类型AS“ sourceToOutputRel_ids_entry_id_entry_ent_1_entries_entry_ent_1_entries_entry_ent_1_entries_entry_ent_entries_ent_1_entries_ent_1_entries_entry_ent_1_entries_entry_ent_entries_1 AS条目_1_文本,条目_1.body_html AS条目_1_body_html,条目_1.user_id AS条目_1_user_id,条目_1.date_posted AS条目_1_date_posted,entries_2.id AS条目_2_id,entries_2.title AS条目_2_title,entries_2.text AS条目_2_text,entries_2.body_html AS条目_2_body_html,entry_2。 er_id AS entries_2_user_id, entries_2.date_posted AS entries_2_date_posted \\nFROM entries JOIN "sourceToOutputRel" ON "sourceToOutputRel".output_article = entries.id JOIN "sourceToOutputRel" ON "sourceToOutputRel".source_article = entries.id JOIN original_tag_registration ON entries.id = original_tag_registration.entry_id JOIN "tagTable" ON "tagTable".id = original_tag_registration.tag_id LEFT OUTER JOIN entries AS entries_1 ON "sourceToOutputRel".output_article = entries_1.id LEFT OUTER JOIN entries AS entries_2 ON "sourceToOutputRel".source_article = entries_2.id \\nWHERE "tagTable".tag = ?' er_id AS条目_2_user_id,entries_2.date_posted AS条目_2_date_posted \\ nFROM条目JOIN“ sourceToOutputRel” ON“ sourceToOutputRel”。 JOIN“ tagTable” ON“ tagTable” .id = original_tag_registration.tag_id LEFT OUTER JOIN条目AS条目_1 ON“ sourceToOutputRel” .output_article =条目_1.id LEFT OUTER JOIN条目AS条目_2 ON“ sourceToOutputRel” .source_article = entry_2.id \\ nWHERE“ tagTable “ .tag =吗? (u'brods',) (u'brods',)

Look at the docs . 看一下文档 Paragraph

Joins to a Target with an ON Clause 通过ON子句加入目标

a_alias = aliased(Address)

q = session.query(User).\
    join(User.addresses).\
    join(a_alias, User.addresses).\
    filter(Address.email_address=='ed@foo.com').\
    filter(a_alias.email_address=='ed@bar.com')

There are multiple join on one table. 一个表上有多个联接。 You already import aliased funciton. 您已经导入了别名函数。 Try this code 试试这个代码

'''
  tags is a list of strings that are supposed to match the Tags.tag strings
'''
from sqlalchemy.orm import aliased

entry_alias = aliased(Entries)
q = SourceToOutputRelation.query.\
     join(Entries.source_entries).\
     join(entry_alias, Entries.output_entries).\
     join(original_tag_registration).\
     join(Tags).\
     filter(Tags.tag == tags[0])
print(q.all())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM