Problem with code of chapter nine of 'Essential SQLAlchemy'

37 views
Skip to first unread message

Decebal

unread,
Aug 12, 2008, 5:57:09 AM8/12/08
to SQLElixir
I want to work with SQLAlchemy and bought the book 'Essential Alchemy'
of O'Reilly. In chapter nine elixir is shown.
I have now the following code in model.py:
#####
from elixir import *

metadata.bind = 'sqlite:////home/cecil/databases/dummy.db'

class Product(Entity):
def __init__(self, sku, msrp):
self.sku = sku
self.msrp = msrp
self.prices = []

has_field('sku', String(20), primary_key = True)
has_field('msrp', Numeric)
has_many('prices', of_kind = 'Price')

class Store(Entity):
def __init__(self, name):
self.name = name

has_field('name', Unicode(255))
has_many('prices', of_kind = 'Price')

class Price(Entity):
def __init__(self, product, store, price):
self.product = product
self.store = store
self.price = price

has_field('price', Numeric, default = 0)
belongs_to('product', of_kind = 'Product')
belongs_to('store', of_kind = 'Store')
#####

And in dummy.py I have:
#####
from elixir import *
from model import *

create_all()

stores = [
Store('Main Store'),
Store('Secondary Store'),
]

products = [
Product('123', 11.22),
Product('456', 33.44),
Product('789', 123.45),
]

prices = [
Price(product = product, store = store, price = 10.00)
for product in products
for store in stores
]

session.flush()
#####

The code in the book did not have the __init__ functions, but without
those it does not work at al.
When I run 'python -i dummy.py' and in the interpreter give:
#####
>>> metadata.bind.echo = True
>>> price = Price.get(1)
#####

I get:
#####
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.5/site-packages/Elixir-0.6.0-py2.5.egg/
elixir/entity.py", line 1002, in get
return cls.query.get(*args, **kwargs)
AttributeError: type object 'Price' has no attribute 'query'
#####

What is happening here?

Gaetan de Menten

unread,
Aug 12, 2008, 6:22:59 AM8/12/08
to sqle...@googlegroups.com
On Tue, Aug 12, 2008 at 11:57 AM, Decebal <CLDWes...@gmail.com> wrote:

> I want to work with SQLAlchemy and bought the book 'Essential Alchemy'
> of O'Reilly. In chapter nine elixir is shown.
> I have now the following code in model.py:

It is missing a call to "setup_all()" after the model is defined (see
below). I supposed the book used Elixir version 0.3.0 or earlier. This
is a requirement which was introduced in version 0.4.

Without this call, Elixir doesn't do its job: create a mapper and
tables for your entities. This also implied that your create_all()
call below wouldn't create any tables in your database.

setup_all()

Hope it helps,

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

Cecil Westerhof

unread,
Aug 12, 2008, 6:52:37 AM8/12/08
to sqle...@googlegroups.com
2008/8/12 Gaetan de Menten <gdem...@gmail.com>:

>> I want to work with SQLAlchemy and bought the book 'Essential Alchemy'
>> of O'Reilly. In chapter nine elixir is shown.
>> I have now the following code in model.py:
>
> It is missing a call to "setup_all()" after the model is defined (see
> below). I supposed the book used Elixir version 0.3.0 or earlier. This
> is a requirement which was introduced in version 0.4.

The book says it works with 0.4 and 0.5.


> Without this call, Elixir doesn't do its job: create a mapper and
> tables for your entities. This also implied that your create_all()
> call below wouldn't create any tables in your database.

Now it works. ;-} And the tables where not created, yes. I was
wondering about that. The dummy.db was created, but it kept empty. Now
it is correctly filled.
Thanks.

There are more errors in the book. (Too many.)

For example it says that after
metadata.bind.echo = True
you should get a lot off explenationary info when you do
price = Price.get(1)

In the code is also price.store_name instead of price.store.name.

And when running the code I get:
#####
/usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
SAWarning: Unicode type received non-unicode bind param value 'Main
Store'
param.append(processors[key](compiled_params[key]))
/usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
SAWarning: Unicode type received non-unicode bind param value
'Secondary Store'
param.append(processors[key](compiled_params[key]))
#####

What do I need to do to get rid of this warning?

--
Cecil Westerhof

Gaetan de Menten

unread,
Aug 12, 2008, 7:10:38 AM8/12/08
to sqle...@googlegroups.com
On Tue, Aug 12, 2008 at 12:52 PM, Cecil Westerhof
<cldwes...@gmail.com> wrote:

> For example it says that after
> metadata.bind.echo = True
> you should get a lot off explenationary info when you do
> price = Price.get(1)

Well, that should be the case indeed. That is, you should see the SQL
that is sent to the DB. Don't you see anything?

> And when running the code I get:
> #####
> /usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
> SAWarning: Unicode type received non-unicode bind param value 'Main
> Store'
> param.append(processors[key](compiled_params[key]))
> /usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
> SAWarning: Unicode type received non-unicode bind param value
> 'Secondary Store'
> param.append(processors[key](compiled_params[key]))
> #####
>
> What do I need to do to get rid of this warning?

pass unicode strings to unicode fields, as for example:

stores = [
Store(u'Main Store'),
Store(u'Secondary Store'),
]

Cecil Westerhof

unread,
Aug 12, 2008, 7:32:37 AM8/12/08
to sqle...@googlegroups.com
2008/8/12 Gaetan de Menten <gdem...@gmail.com>:
>
> On Tue, Aug 12, 2008 at 12:52 PM, Cecil Westerhof
> <cldwes...@gmail.com> wrote:
>
>> For example it says that after
>> metadata.bind.echo = True
>> you should get a lot off explenationary info when you do
>> price = Price.get(1)
>
> Well, that should be the case indeed. That is, you should see the SQL
> that is sent to the DB. Don't you see anything?

No. After 'python -i dummy.py' I get the following dialog when
inputting some commands:
#####
$ python -i dummy.py


>>> metadata.bind.echo = True
>>> price = Price.get(1)

>>> price.store.name
u'Main Store'
>>> session.commit()
2008-08-12 13:28:18,568 INFO sqlalchemy.engine.base.Engine.0x..0c COMMIT
#####


>> And when running the code I get:
>> #####
>> /usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
>> SAWarning: Unicode type received non-unicode bind param value 'Main
>> Store'
>> param.append(processors[key](compiled_params[key]))
>> /usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py:237:
>> SAWarning: Unicode type received non-unicode bind param value
>> 'Secondary Store'
>> param.append(processors[key](compiled_params[key]))
>> #####
>>
>> What do I need to do to get rid of this warning?
>
> pass unicode strings to unicode fields, as for example:
>
> stores = [
> Store(u'Main Store'),
> Store(u'Secondary Store'),
> ]

That works. I think I should send some errata to O'Reilly. ;-}

Thanks again.

--
Cecil Westerhof

Reply all
Reply to author
Forward
0 new messages