M
M
MoonMeTwice2021-01-26 18:56:21
Python
MoonMeTwice, 2021-01-26 18:56:21

How to update data in an associative table?

There are the following models in alchemy:

RecordAndTask = Table('record_and_task', Base.metadata, 
                      Column('record_id', String, ForeignKey('record.id'), primary_key=True),
                      Column('task_id', String, ForeignKey('task.id'), primary_key=True))

RecordAndWidget = Table('record_and_widget', Base.metadata,
                        Column('record_id', String, ForeignKey('record.id'), primary_key=True),
                        Column('widget_id', String, ForeignKey('widget.id'), primary_key=True))


class Record(Base):
    __tablename__ = 'record'

    id = Column(String, primary_key=True)

    tasks = relationship('Task', secondary=RecordAndTask, backref=backref('records', lazy='dynamic'), lazy='dynamic')
    widgets = relationship('Widget', secondary=RecordAndWidget, backref=backref('records', lazy='dynamic'), lazy='dynamic')


class Task(Base):
    __tablename__ = 'task'

    id = Column(String, primary_key=True)
    status = Column(String)

    widget_id = Column(String, ForeignKey('widget.id'))


class Widget(Base):
    __tablename__ = 'widget'

    id = Column(String, primary_key=True)

    tasks = relationship('Task', backref='widget', lazy='dynamic')


The tables contain the following entries. record
table :
id='record_1'             
id='record_2'                         
id='record_3'


task table :
id='task_1', status='done', widget_id='widget_1'  
id='task_2', status='failed', widget_id='widget_3'
id='task_3', status='done', widget_id='widget_2'


Widget table :
id='widget_1'
id='widget_2'
id='widget_3'


record_and_task table :
record_id='record_1', task_id='task_1'
record_id='record_1', task_id='task_2'
record_id='record_2', task_id='task_3'


record_and_widget table :
record_id='record_1', widget_id='widget_1'
record_id='record_1', widget_id='widget_3'
record_id='record_2', widget_id='widget_2'


It is necessary to execute the task (Task) again if its status (status) was not 'done'. Accordingly, in our case, we re-execute the task with task_id=2.
After the task is completed, the Widget may change. Accordingly, what is the question. How should these updates be made (data changes in the Task) so that they affect the record_and_widget table.
In other words, if a Widget has changed in a Task, that Widget must change wherever it occurs. In this particular case, it's the record_and_widget table. As a result, you should get the following data in the tables:

Task table:
id='task_1', status='done', widget_id='widget_1'  
id='task_2', status='done', widget_id='widget_2'    <- Изменился статус и id виджета
id='task_3', status='done', widget_id='widget_2'


record_and_widget table:
record_id='record_1', widget_id='widget_1'
record_id='record_1', widget_id='widget_2'          <- Должен измениться id и тут
record_id='record_2', widget_id='widget_2'


I tried to add various onupdate, cascade in the ForeignKey parameters, columns, but nothing happens.
How to do it right? It seems to be some kind of standard question for working with connections, but the correct work does not come out.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question