I have two models in SQLAlchemy that I had automatically joining on a foreign key, like so:
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)
name = Column(String(300), nullable=False)
metadata_id = Column(Integer, nullable=True, index=True)
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True)
name = Column(String(300), nullable=False)
parent_metadata_id = \
Column(ForeignKey('parents.metadata_id'),
nullable=True, primary_key=True)
parent = relationship(u'Parent')
This worked fine and I could get easily access a parent from its children. Now, for technical reasons beyond the scope of this question, I had to get rid of the foreign key in my db. I've tried to get around that in SQLAlchemy, but none of the replacement code (using primaryjoins, or backrefs) has worked. I saw another answer here that just says to lie to SQLAlchemy and tell it I have the foreign key relationship, but this makes Alembic try to create the foreign key relationship on every new revision I autogenerate, which is really annoying. What's the right way to do this?