getting started with elixir in the model

0 views
Skip to first unread message

Richard Harding

unread,
Mar 14, 2007, 12:20:28 PM3/14/07
to turbo...@googlegroups.com
I'm trying to start up a small test project with SA and decided to toss
Elixir in the mix as well.

I've just created a simple quotes model and if I populate the db I can
get records fine. sql create worked all peachy. What I can't get, is how
to save records created. I just created a method in the model:

class UserQuotes(Entity):
has_field('project', Unicode(100), primary_key=True)
has_field('user_name', Unicode(100))
has_field('user_quote', Unicode)
has_field('year', Integer)

@staticmethod
def load_default_data():
quote1 = UserQuotes(project="Test1",
user_name="Rick",
user_quote="This is the other quote",
year="2007")
quote2 = UserQuotes(project="Test2",
user_name="Erica",
user_quote="This is my quote for it",
year="2006")

objectstore.flush()

Ok, looks good, except nothing makes it to the database. My controller
method is just:

@expose()
def load_default(self):
UserQuotes.load_default_data()
return dict()

I don't get any errors, but nadda in the db. So what am I missing? using
objectstore.flush() is the only method of saving the created objects in
the tutorial. In their sample TG app they only fetch rows and there's no
code/method for editing/saving new movie records.

Is there any other sample code out there I can get my hands on?

Thanks for the help.

Rick

Richard Harding

unread,
Mar 14, 2007, 1:14:41 PM3/14/07
to turbo...@googlegroups.com
Richard Harding wrote:
> I'm trying to start up a small test project with SA and decided to toss
> Elixir in the mix as well.
>
> I've just created a simple quotes model and if I populate the db I can
> get records fine. sql create worked all peachy. What I can't get, is how
> to save records created. I just created a method in the model:
>
*snip*

So I decided to try to just do this with SA and changed everything
around with the same results. It just won't write to the db. I don't get
it. In my dev.cfg I have sqlalchemy.echo = 1

My SA version of the model/method:
Model
----------------------------------
quotes_table = Table("avwsystems_model_userquotes", metadata,
Column("project", Unicode(100), primary_key=True),
Column("user_name", Unicode(100)),
Column('user_quote', Unicode),
Column("year", Integer)
)

class Quotes(object):
pass

quote_mapper = mapper(Quotes, quotes_table)

Controller
-------------------------------------
@expose()
def load_default(self):
session = database.session
q = Quotes()
q.project = "Test1"
session.save(q)

return 'done'

The only output to the terminal is the GET request. My display is just
Done printed in the browser.

Sorry to make it seem Elixir specific, but it seems to be more general
than that.

Thanks for any help.

Rick

Kevin Horn

unread,
Mar 15, 2007, 12:31:39 PM3/15/07
to turbo...@googlegroups.com
Wouldn't you need to do something like:

newobject = NewObject(....)
session.save(newobject) # associates the object with the session
session.flush() # sends the data to the DB

or does TG have some "magic" that handles the session.flush() automatically?

I'm afraid I haven't used SA that much yet...

Kevin Horn

Richard Harding

unread,
Mar 15, 2007, 12:35:09 PM3/15/07
to turbo...@googlegroups.com
Kevin Horn wrote:
> Wouldn't you need to do something like:
>
> newobject = NewObject(....)
> session.save(newobject) # associates the object with the session
> session.flush() # sends the data to the DB
>
> or does TG have some "magic" that handles the session.flush() automatically?
>
> I'm afraid I haven't used SA that much yet...
>
> Kevin Horn

I spend some time and T` on IRC helped me get it all squared away. My
method now looks like this and it works. Much nicer/prettier than it
originally was.

@staticmethod
def load_quote():
q = Quotes()
q.project = 'testing'
q.flush()

So from the best I can tell, the .save() is auto handled
somewhere/somehow and I have just have to do the flush() manually
whenever I want to commit the change.

Rick

Reply all
Reply to author
Forward
0 new messages