How to save to two tables using one SQLAlchemy model

Posted by Oatman on Stack Overflow See other posts from Stack Overflow or by Oatman
Published on 2010-05-19T11:07:44Z Indexed on 2010/05/19 11:40 UTC
Read the original article Hit count: 172

Filed under:
|
|
|

I have an SQLAlchemy ORM class, linked to MySQL, which works great at saving the data I need down to the underlying table. However, I would like to also save the identical data to a second archive table.

Here's some psudocode to try and explain what I mean

my_data = Data() #An ORM Class
my_data.name = "foo"

#This saves just to the 'data' table
session.add(my_data)

#This will save it to the identical 'backup_data' table
my_data_archive = my_data
my_data_archive.__tablename__ = 'backup_data'
session.add(my_data_archive)

#And commits them both
session.commit()

Just a heads up, I am not interested in mapping a class to a JOIN, as in: http://www.sqlalchemy.org/docs/05/mappers.html#mapping-a-class-against-multiple-tables

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy