Forward to list for reference...

---------- Forwarded message ----------
From: Gaetan de Menten <[EMAIL PROTECTED]>
Date: Thu, Jun 26, 2008 at 2:20 PM
Subject: Re: [elixir] Re: elixir metadata.drop_all and metadata.create_all issue
To: Jay Decker <[EMAIL PROTECTED]>


On Wed, Jun 25, 2008 at 9:44 PM, Jay Decker <[EMAIL PROTECTED]> wrote:
> Hey Gaëtan de Menten,
>
> Here is the zip.
>
> Thank you,
> Jay
>
> On Wed, Jun 25, 2008 at 12:03 PM, Gaetan de Menten <[EMAIL PROTECTED]>
> wrote:
>>
>> On Wed, Jun 25, 2008 at 4:03 PM, lilo <[EMAIL PROTECTED]> wrote:
>> >
>> > I have tried setup_all() at module level and still getting the same
>> > error.
>>
>> after importing classes and before load_data?
>>
>> If so, send me (not to the list) an archive with your code, I'll see
>> what you're doing wrong.

Doh! Sorry, I read that traceback/exception reaaaally too quickly. It
had nothing to do with setup_all().

Your problem come from non-sense/typos in your code:

>    sess1 = create_session(bind=m1)
>    sess2 = create_session(bind=m2)
>    sess1.save(m1)
>    sess2.save(m2)

Trying to save the engines (m1 and m2) in the session has no sense...
Sessions are meant to store "mapped" objects... So dm1 and dm2 would
be better here. Then you have the problem that the b table doesn't
exist in the m2 engine (but in the n* engines), so those 4 lines
should read:

sess1 = create_session(bind=m1)
sess2 = create_session(bind=n1)
sess1.save(dm1)
sess2.save(dm2)

Now, there is one more mistake, but that one is more subtle, so I
forgive you (;-)): you are using manual sessions (ie create_session
stuff), and you tell nowhere to your Elixir entities, they shouldn't
use the default contextual session (elixir.session), so you'll have a
session conflict (one instance cannot be added to two different
sessions at the same time).

There are several ways to fix that: with using_options in each your
entities, with __session__ = None in each of your model files, or by
directly trashing the default session: elixir.session = None

Attached are the corrected files...

-- 
Gaëtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

### file a.py

from elixir import *
from sqlalchemy import MetaData

a_metadata = MetaData()
__metadata__ = a_metadata
# alternate way to tell Elixir not to use the default session (for entities in
# this module)
#__session__ = None

class A(Entity):
   using_options(tablename='a')
   aname = Field(String(30), primary_key=True)

### file b.py

from elixir import *
from sqlalchemy import MetaData

b_metadata = MetaData()
__metadata__ = b_metadata

# alternate way to tell Elixir not to use the default session (for entities in
# this module)
#__session__ = None

class B(Entity):
   using_options(tablename='b')
   bname = Field(String(30),  primary_key=True)

#!/usr/bin/env python

from sqlalchemy import create_engine
from elixir import *

import elixir
elixir.session = None

import b
import a

m1 = create_engine("sqlite:///", echo=True)
m2 = create_engine("sqlite:///", echo=True)

n1 = create_engine("sqlite:///", echo=True)
n2 = create_engine("sqlite:///", echo=True)

setup_all()

# create tables
for db in (m1, m2):
    a.a_metadata.drop_all(bind=db)
    a.a_metadata.create_all(bind=db)

for db in (n1, n2):
    b.b_metadata.drop_all(bind=db)
    b.b_metadata.create_all(bind=db)

#!/usr/bin/env python

from sqlalchemy.orm import create_session

from c import m1,m2,n1,n2
from a import A
from b import B

def load_data():
    dm1 = A(aname="a")
    dm2 = B(bname="b")
    sess1 = create_session(bind=m1)
    sess2 = create_session(bind=n1)
    sess1.save(dm1)
    sess2.save(dm2)
    sess1.flush()
    sess1.clear()
    sess2.flush()
    sess2.clear()

if __name__ == '__main__':
    load_data()

Reply via email to