On Wed, Dec 2, 2009 at 21:57, Peter <[email protected]> wrote:

> The following runs without errors, creates the database but not the
> table ( and not the objects ) :

Your code creates the table correctly here (FWIW, it's named "__main__account").

> from elixir import  setup_all, create_all, drop_all, Entity, session,
> Field, Unicode, String, metadata
> from sqlalchemy import create_engine
> from sqlalchemy.orm import sessionmaker
>
>
> Session = sessionmaker()
> engine = create_engine('mysql://r...@localhost/tmp', echo=False)

Using echo=True, usually makes debugging easier...

> connection = engine.connect()
> session = Session(bind=connection)
> metadata.bind=engine
> connection.execute('CREATE DATABASE IF NOT EXISTS TEST')
> connection.execute('USE TEST')
>
>
> class Account(Entity):
>
>  name = Field(String(30))
>  type = Field(String(30))
>  code = Field(String(30))
>  desc = Field(String(50))
>
> drop_all(checkfirst=True)

This does not make sense at this point, because SQLAlchemy can only
drop what it knows about, ie the tables in the metadata and at this
point, the metadata is empty, because setup_all() did not populate it
yet.

> setup_all()
> create_all()
>
> a = Account()
> session.commit()
>
> Am I mixing up Elixir and SqlAlchemy stuff ?

Yes :). The problem is that you are both using Elixir's builtin
session (because you are not passing any session option one way or
another to the Entity), AND defining a new session, so what happens is
that "session.commit()" commits your own session, which is empty.
There are two ways to fix this:
1) use elixir default session all the way
2) use your own session but tell Elixir about it (either with
using_options, or __session__ at the module level). In that case,
don't forget to add: "session.add(a)" before "session.commit()" (since
you didn't define a ScopedSession).

Hope it helps,
-- 
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.


Reply via email to