Skip to content

Instantly share code, notes, and snippets.

@npilon
Created August 26, 2019 23:17
Show Gist options
  • Save npilon/fb380e2f32d52d9c5442a9e2d1ff49d1 to your computer and use it in GitHub Desktop.
Save npilon/fb380e2f32d52d9c5442a9e2d1ff49d1 to your computer and use it in GitHub Desktop.
from sqlalchemy import Column, ForeignKey, Integer, and_, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
backref, foreign, relationship, scoped_session, sessionmaker
)
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_1_id = Column(Integer, ForeignKey(A.id))
a_2_id = Column(Integer, ForeignKey(A.id))
a_1 = relationship(
A,
primaryjoin=and_(a_1_id == foreign(A.id)),
backref=backref(
name='a_1s', lazy='dynamic', uselist=True
),
uselist=False,
)
a_2 = relationship(
A,
primaryjoin=and_(a_2_id == foreign(A.id)),
backref=backref(
name='a_2s', lazy='dynamic', uselist=True
),
uselist=False,
)
FooSession = scoped_session(sessionmaker())
engine = create_engine('sqlite:///dynamicfail.sqlite')
FooSession.configure(bind=engine)
engine.bind = Base.metadata
FooSession.query(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment