chaouche yacine wrote:
> --- On Sun, 11/22/09, Conor <conor.edward.da...@gmail.com> wrote:
>
>   
>> There is a problem with your code when the tag is in the
>> cache: if the
>> tag is added to the session via session.add or a relation
>> "add" cascade,
>> SQLAlchemy will try to INSERT the tag into the database on
>> the next
>> flush. 
>> -Conor
>>     
>
> I don't know, it seems not. I created a new empty Tags table, I create a new 
> Pylons tag, and it creates it only on the first flush. If I add it a second 
> time to the session, and reflush it, it won't try to re-insert it in the db. 
> Here's my ipython session : 
>
> In [1]: from someproject.model.tag import *
>
> In [2]: setup_all()
>
> In [3]: create_all()
> 12:59:34,878 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] SHOW VARIABLES 
> LIKE 'sql_mode'
> 12:59:34,882 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ()
> 12:59:34,886 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] DESCRIBE `Tags`
> 12:59:34,886 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ()
> 12:59:34,888 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ROLLBACK
> 12:59:34,889 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] 
> CREATE TABLE `Tags` (
>       id INTEGER NOT NULL AUTO_INCREMENT, 
>       name VARCHAR(64), 
>       PRIMARY KEY (id)
> )
>
>
> 12:59:34,907 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ()
> 12:59:34,959 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] COMMIT
>
> In [4]: pylons = Tag("Pylons")
> 12:59:59,171 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] BEGIN
> /home/chaouche/PYTHONENV/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/engine/default.py:230:
>  SAWarning: Unicode type received non-unicode bind param value 'Pylons'
>   param.append(processors[key](compiled_params[key]))
> 12:59:59,172 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] SELECT `Tags`.id 
> AS `Tags_id`, `Tags`.name AS `Tags_name` 
> FROM `Tags` 
> WHERE `Tags`.name = %s 
>  LIMIT 0, 1
> 12:59:59,172 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ['Pylons']
> not in the database
>
> In [5]: pylons = Tag("Pylons")
>
> In [6]: session.add(pylons)
>
> In [7]: session.flush
> Out[7]: <bound method ScopedSession.do of 
> <sqlalchemy.orm.scoping.ScopedSession object at 0x98572ac>>
>
> In [8]: session.flush()
> 13:00:14,116 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] INSERT INTO 
> `Tags` (name) VALUES (%s)
> 13:00:14,116 INFO  [sqlalchemy.engine.base.Engine.0x...6fec] ['Pylons']
>
> In [9]: session.add(pylons)
>
> In [10]: session.flush()
>
> In [11]: pylons = Tag("Pylons")
>
> In [12]: pylons
> Out[12]: <Tag Pylons>
>
> In [13]: session.add
> session.add      session.add_all  
>
> In [13]: session.add(pylons)
>
> In [14]: session.flush()
>
> In [15]: 
>   

I think the problem is still there, but it is only exposed if the
session that inserts the "Pylons" tag is different from the session that
gets the tag from a cache hit. If you are indeed using Pylons, you will
get a new session for each HTTP request, so you will see the error in
that situation.

If your "session" object was created by sessionmaker(), you should be
able to trigger the problem by adding "session.remove()" between lines
10 and 11 in your test case above.

-Conor

--

You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=.


Reply via email to