I have tables like this:
from sqlalchemy import Column, Integer, select
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Blah(Base):
__tablename__ = 'Blah'
container_id = Column(Integer, primary_key=True)
blah_id = Column(Integer, primary_key=True)
gprop = Column(Integer, index=True, nullable=False)
class GProp(Base):
__table__ = select([
Blah.container_id, Blah.gprop
]).group_by(
Blah.container_id, Blah.gprop
).alias()
As it is GProp.__table__.primary_key picks up the container_id column, but I can’t find a way to make the gprop column part of the primary key. This breaks my queries, SQLAlchemy will only load one GProp per container.
Unsuccessful fixes so far: Adding Column attributes is forbidden because it redefines existing columns, adding a PrimaryKeyConstraint to __table_args__ has no effect, and monkey-patching __table__.primary_key gets caught by just the wrong assertion.
OK, adding
does the job, even though it isn’t reflected on
__table__.primary_key.