简体   繁体   中英

SQLAlchemy many-to-many without foreign key

Could some one help me figure out how should i write primaryjoin/secondaryjoin on secondary table that lacking one ForeignKey definition. I can't modify database itself since it's used by different application.

from sqlalchemy import schema, types, func, orm
from sqlalchemy.dialects.postgresql import ARRAY

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'atab'
    id = schema.Column(types.SmallInteger, primary_key=True)

class B(Base):
    __tablename__ = 'btab'
    id = schema.Column(types.SmallInteger, primary_key=True)

    a = orm.relationship(
        'A', secondary='abtab', backref=orm.backref('b')
    ) 

class AB(Base):
    __tablename__ = 'abtab'
    id = schema.Column(types.SmallInteger, primary_key=True)
    a_id = schema.Column(types.SmallInteger, schema.ForeignKey('atab.id'))
    b_id = schema.Column(types.SmallInteger)

I've tried specifing foreign on join condition:

a = orm.relationship(
    'A', secondary='abtab', backref=orm.backref('b'),
    primaryjoin=(id==orm.foreign(AB.b_id))
) 

But received following error:

ArgumentError: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition '"atab".id = "abtab"."a_id"' on relationship Category.projects.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation. To allow comparison operators other than '==', the relationship can be marked as viewonly=True.

You can add foreign_keys to your relationship configuration. They mention this in a mailing list post :

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    logon = Column(String(10), primary_key=True)
    group_id = Column(Integer)

class Group(Base):
    __tablename__ = 'groups'
    group_id = Column(Integer, primary_key=True)
    users = relationship('User', backref='group',
        primaryjoin='User.group_id==Group.group_id',
        foreign_keys='User.group_id')

engine = create_engine('sqlite:///:memory:', echo=True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

u1 = User(logon='foo')
u2 = User(logon='bar')
g = Group()
g.users = [u1, u2]
session.add(g)
session.commit()
g = session.query(Group).first()
print([user.logon for user in g.users])

output:

['foo', 'bar']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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