Model:
class Example(Base):
__tablename__ = "example"
create_time = Column(DateTime, server_default=func.now())
time_stamps = Column(MutableList.as_mutable(ARRAY(DateTime)), server_default="{}")
update_time = Column(DateTime, server_default=func.now())
Now when I insert new example, I need to append the create_time
of new example into the time_stamps
ARRAY, then I need to sort it to get the newest time and that time set as a new update_time
.
I managed to do it separately
def update_record(db: Session, create_time: datetime, db_record: Example):
db_record.time_stamps.append(create_time)
sorted_times = sorted(db_record.time_stamps, reverse=True)
db_record.update_time = sorted_times[0]
db_record.time_stamps = sorted_times
db.commit()
But I need to do it atomically using INSERT ON CONFLICT UPDATE
clause.
So far I have:
db_dict = {"create_time": record.create_time,
"time_stamps": [record.create_time],
"update_time": record.create_time}
stm = insert(Example).values(db_dict)
do_update_stm = stm.on_conflict_do_update(constraint='my_unique_constraint',
set_=dict(??)
My question is how to access and append to values of the original conflict row in set_
inside conflict_do_update
in SQLAlchemy?
Thanks
question from:
https://stackoverflow.com/questions/65901800/how-to-append-to-a-list-in-on-conflict-do-update-in-sqlalchemy 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…