I am working on an app using python3 and SqlAlchemy for SQLite3 database management. I have some tables that have a Many to Many relationship. I've created an association table to handle this relationship.
Class Machine(Base):
__tablename__ 'machine'
machine_ID = Column(Integer, primary_key=True)
etc...
Class Options(Base):
__tableName__ 'options'
options_ID = Column(Integer, primary_key=True)
etc...
The association table
Machine_Options = table('machine_options', Base.metadata,
Column('machine_FK', Integer, ForeignKey('machine.machine_ID'),
primary_key=True),
Column('options_FK',Integer, ForeignKey('options.options_ID'),
primary_key=True))
All the items for the Machine and Options are inserted independently. When I want to associate a machine with an option I use an append query which works very well.
My problem is when I want to break this association between a machine and an option. I have tried a direct row deletion from the association table using a FILTER() clause on the machine_FK and the options_FK but SqlAlchemy gives me an error informing me that 'Machine_Options' table has no field 'machine_FK'. It seems that SqlAlchemy does not map association tables. I have tried to remove the row from 'Machine_Options' indirectly using joins with the machine and options table but received another error that I can not delete or update using joins.
I am looking for the code to only delete a row from the association table without affecting the original machine or options table.
So far my internet search has been fruitless.
# machine
class Machine(Base):
__tablename__ = 'machine'
machine_ID = Column(Integer, primary_key=True)
machine_Type = Column(Integer, nullable=False)
machine_model = Column(String(10), nullable=False)
machine_Weight = Column(Integer)
machine_price = Column(Float(), nullable=False)
saleRel = relationship('Sale', secondary='sale_product')
children = relationship("Options",
secondary='machine_options',
back_populates="parents")
# options
class Options(Base):
__tablename__ = 'options'
options_ID = Column(Integer, primary_key=True)
options_category = Column(String(10), nullable=False)
options_Price = Column(Float(), nullable=False)
options_Type_FK = Column(Integer, ForeignKey('options_Type.options_Type_ID'))
parents = relationship("Machine",
secondary=Machine_Options, back_populates="children")
#machine_options
Machine_Options = Table('machine_options', Base.metadata,