ok - I understand and have made the changes as suggested. The new script lives here:
http://bitbucket.org/dls/elixir_dump_test/src/tip/dumpTest.py and looks like this: from elixir import * dict= {} class User(Entity): using_options(tablename='User') name = Field(String, unique=True) expenses = OneToMany('Expense') def __repr__(self): return "<User ('%s', '%s')>" % (self.id, self.name) class Expense(Entity): using_options(tablename='Expense') user = ManyToOne('User') amount = Field(Float) def __repr__(self): return "<Expense ('%s', '%s')>" % (self.id, self.amount) # connect connString = 'sqlite:///' + 'dumpTest.db' metadata.bind = connString setup_all() create_all() # create some data u1 = User(name='John Doe') u2 = User(name='Jane Doe') e1 = Expense(user=u1, amount='15.01') e2 = Expense(user=u2, amount='50.25') session.commit() # dump to dict dict['User'] = User.query.all() print dict['User'] dict['Expense'] = Expense.query.all() print dict['Expense'] # close session session.close() # connect to db #2 connString = 'sqlite:///' + 'dumpTest2.db' metadata.bind = connString setup_all() create_all() for i in dict['User']: u = User() u.name = i.name # works u.expenses = i.expenses # does not work for i in dict['Expense']: e = Expense() e.amount = i.amount # works e.user = i.user # does not work session.close() cleanup_all() but triggers this error: sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <User at 0x104fa30> is not bound to a Session; lazy load operation of attribute 'expenses' cannot proc eed on line 53: u.expenses = i.expenses # does not work On Sep 8, 4:43 am, Gaetan de Menten <[email protected]> wrote: > On Wed, Sep 8, 2010 at 00:14, dls <[email protected]> wrote: > > All - I'm working on a simple migration script for Elixir based > > databases, and I've run into some issues. While I was creating a demo > > script to illustrate these issues so I could post to this group, I ran > > into another problem that I think may be related. Below is a simple > > script that creates some entries in an Elixir/sqlite database, dumps > > them into a RAM structure, and then attempts to load them into a > > different database. In this sample script, both databases use the same > > database schema (version). > > > Also, the script can be viewed/downloaded here: > > >http://bitbucket.org/dls/elixir_dump_test/src/tip/dumpTest.py > > > The code is: > > > #************************************************************************** > > ** > > from elixir import * > > > dict= {} > > > class User(Entity): > > using_options(tablename='User') > > name = Field(String, unique=True) > > expenses = OneToMany('Expense') > > > def __repr__(self): > > return "<User ('%s', '%s')>" % (self.id, self.name) > > > class Expense(Entity): > > using_options(tablename='Expense') > > user = ManyToOne('User') > > amount = Field(Float) > > > def __repr__(self): > > return "<Expense ('%s', '%s')>" % (self.id, self.amount) > > > # connect > > connString = 'sqlite:///' + 'dumpTest.db' > > metadata.bind = connString > > setup_all() > > create_all() > > > # create some data > > u1 = User(name='John Doe') > > u2 = User(name='Jane Doe') > > e1 = Expense(user=u1, amount='15.01') > > e2 = Expense(user=u2, amount='50.25') > > session.commit() > > > # dump to dict > > > dict['User'] = User.query.all() > > print dict['User'] > > dict['Expense'] = Expense.query.all() > > print dict['Expense'] > > > # close session > > session.close() > > cleanup_all() > > Here is the problem. After using cleanup_all, you cannot use any > entities you have used so far again, because they were all "unmapped", > so you basically have simple python objects without any database link. > It is intended for testing purposes only: you cleanup everything so > that one test does not leak anything to the next test. session.close() > should be enough in your case I think. > > > # connect to db #2 > > connString = 'sqlite:///' + 'dumpTest2.db' > > metadata.bind = connString > > setup_all() > > And this does nothing since cleanup_all cleared the list of entities. > So yeah, it sets up all entities you have defined since that call to > setup_all... That is: nothing. > > > create_all() > > > for i in dict['User']: > > u = User() > > u.name = i.name # works > > It only *seems* to work... SQLAlchemy's machinery is not triggered as > you would expect, since the class is "unmapped". > > -- > Gaëtan de Menten -- You received this message because you are subscribed to the Google Groups "SQLElixir" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlelixir?hl=en.
