On Sun, Mar 7, 2010 at 23:28, ktx <[email protected]> wrote:
> Hi all,
>
> I am using elixir for a project over 2years now. I have to move to 0.6
> (old, I know, but it's now in lenny). Some (all db :-/) of the code
> breaks down. I followed the Migration tutorial (commit, flush ...) but
> I am still getting the same error.
>
> from elixir import *
>
> metadata.bind = 'sqlite://'
> metadata.bind.echo = True
>
> class Movie(Entity):
>    title = Field(Unicode(30), primary_key=True)
>    year = Field(Integer)
>    description = Field(UnicodeText)
>    characters = OneToMany('Character', inverse='movie')
>
> class Character(Entity):
>    movie = ManyToOne('Movie', primary_key=True)
>    name = Field(Unicode(60), primary_key=True)
>
> setup_all()
> create_all()

Here is what happens:

> matrix = Movie(title='Matrix')

matrix is automatically added to the session (this is normal behavior
when you use Elixir's default session).

> Character.query.all()   # or Movie.query.all() # (*)

since the default session of SQLAlchemy is "autoflush=True", any query
to the database flushes the session. Ie, "matrix" is sent to the
database.

> rabbit = Character(name='The White Rabbit')

as above, rabbit is added to the session

> matrix.characters.append(rabbit) # long exception ....

since the ORM detects that matrix is already stored in the database,
it issues a query refresh its "characters" relation *before* appending
to it, however it also flushes "rabbit" in the process. And since
rabbit has not been assigned a movie yet, the DB complains...

> session.commit()

You can fix this in different ways. One option would be to configure
the session differently:
session.configure(autoflush=False)

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

Reply via email to