python - PostgreSQL table inheritance and moving rows with SQLAlchemy -
in app (python, postgres, sqlalchemy) had 1 large table tasks. app works recent 1k rows in tasks, selecting , updating rows. such frequent operations slow due size of tasks decided split table 2 tasks tasks_all, table tasks inheried tasks_all (postgresql feature).
thus app can work small table fast , when old data, can work large table, includes rows self , it's successor.
here simplified classes tables:
class taskbase: def __init__(self, id, parent_id, data): self.id = id self.parent_id = parent_id self.data = data def __repr__(self): return '<task: {} {}>'.format(self.id, self.data) class task(taskbase, base): __tablename__ = 'tasks' id = column(int, primary_key=true) parent_id = column(int, foreignkey('tasks.id')) data = column(text) children = relationship("task", backref=backref('parent', remote_side=[id])) class taskall(taskbase, base): __tablename__ = 'tasks_all' id = column(int, primary_key=true) parent_id = column(int, foreignkey('tasks_all.id')) data = column(text) children = relationship("taskall", backref=backref('parent', remote_side=[id])) to keep tasks table small, need time time move rows tasks tasks_all. , need move them back. following code how tried implement moving.
def move_group(ids): query = session.query(taskall).filter(taskall.id in ids) db_session.s.execute(insert(task).from_select(( taskall.id, taskall.parent_id, taskall.data, ), query)) print("insert select successful") query.delete() print("delete successful") but doesn't work because table tasks_all parent tasks, delete tasks_all deletes tasks. resulting sql query is:
delete tasks_all tasks_all.id in (...); but perform operation need, query shuld be
delete tasks_all tasks_all.id in (...); as far find sqlalchemy knows nothing only. have use raw sql queries sqla skipping orm part, or maybe there better solution move rows 1 table another?
Comments
Post a Comment