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

时间:2016-03-29 20:34:40

标签: python join sqlalchemy

我正在尝试在具有两个foriegn键到同一个表的表上进行连接。也就是说,sourceToOutputRelation指向Entries两次,如代码所示。此外,条目有标签。我正在尝试进行连接,以便获得具有所有给定标记的所有sourceToOutputRelation(通过条目)。我只是想了解连接(过滤工作,我认为)。这是我加入和过滤的代码。 :

'''
  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())

以下是我的模型定义:

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') 

我收到此错误:

  

OperationalError:(OperationalError)模糊列名:   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&#34 ;,   " 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.user_id AS   entries_2_user_id,entries_2.date_posted AS entries_2_date_posted   \ nFROM条目JOIN" sourceToOutputRel"上   " sourceToOutputRel" .output_article = entries.id加入   " 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条目AS entries_1   ON" sourceToOutputRel" .output_article = entries_1.id LEFT OUTER JOIN   条目AS entries_2 ON" sourceToOutputRel" .source_article =   entries_2.id \ nWHERE" tagTable" .tag =?' (U' brods',)

1 个答案:

答案 0 :(得分:2)

查看docs。 段落

  

使用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')

一张桌子上有多个连接。 您已导入别名功能。 试试这段代码

'''
  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())