Say I have two tables, foo and bar. Both have primary keys. I want to set it up in SQLAlchemy so that the combined set of foo.id and bar.id is unique. How would I do that?
I tried adding another table containing only the primary keys and having foreign keys in foo and bar, like so:
class foo(Base):
__tablename__ = 'foo'
id = Column(Integer, ForeignKey('primary_keys.id'), primary_key=True)
class bar(Base):
__tablename__ = 'bar'
id = Column(Integer, ForeignKey('primary_keys.id'), primary_key=True)
class primary_keys(Base):
__tablename__ = 'primary_keys'
id= Column(Integer, primary_key=True)
But it gave me this error:
FlushError: Instance has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such aswithin a load() event.
Is there maybe a better solution for what I'm trying to do?
EDIT: I'm using a sqlite db.
create_all
function? – user25064