22

I'm writing a Pylons app, and am trying to create a simple backup system where every table is serialized and tarred up into a single file for an administrator to download, and use to restore the app should something bad happen.

I can serialize my table data just fine using the SqlAlchemy serializer, and I can deserialize it fine as well, but I can't figure out how to commit those changes back to the database.

In order to serialize my data I am doing this:

from myproject.model.meta import Session
from sqlalchemy.ext.serializer import loads, dumps
q = Session.query(MyTable)
serialized_data = dumps(q.all())

In order to test things out, I go ahead and truncation MyTable, and then attempt to restore using serialized_data:

from myproject.model import meta
restore_q = loads(serialized_data, meta.metadata, Session)

This doesn't seem to do anything... I've tried calling a Session.commit after the fact, individually walking through all the objects in restore_q and adding them, but nothing seems to work.

What am I missing? Or is there a better way to do what I'm aiming for? I don't want to shell out and directly touch the database, since SqlAlchemy supports different database engines.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
swilliams
  • 48,060
  • 27
  • 100
  • 130

1 Answers1

14

You have to use Session.merge() method instead of Session.add() to put deserialized object back into the session.

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • 1
    Where is the `Session.add()` call in the OP that needs replacing? Are there lines of code missing? – Efren Dec 21 '18 at 02:20
  • 1
    @Efren Missing lines are described in OP with "walking through all the objects in restore_q and adding them" phrase. – Denis Otkidach Dec 21 '18 at 14:51
  • note the OP links to docs for Expression Serializer Extension in SLQAlchemy v1.3 - in v1.4 docs for the same page there is a warning about security consideration related to use of pickle... I am working with untrusted input data in db, so will be looking elsewhere for backup/restore methodology. Furthermore, [in v2.0 docs](https://docs.sqlalchemy.org/en/20/core/serializer.html) it is flagged as: Legacy Feature The serializer extension is legacy and should not be used for new development. – Samuel Freeman Mar 15 '23 at 14:00