Thaks for your answers but maybe I haven't well explained the problem
Please look at this examples:
paster shell
from sambadm.model.smbserver import *
import transaction
s=SambaShares(nome='testunique',path='/tmp/testunique')
DBSession.add(s)
try:
transaction.commit()
except:
print 'error'
09:15:28,179 INFO [sqlalchemy.engine.base.Engine.0x...c26c] BEGIN
09:15:28,182 INFO [sqlalchemy.engine.base.Engine.0x...c26c] select
nextval('"samba_shares_id_seq"')
09:15:28,182 INFO [sqlalchemy.engine.base.Engine.0x...c26c] None
/home/nicola/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.5.0rc4-
py2.6.egg/sqlalchemy/engine/default.py:241: SAWarning: Unicode type
received non-unicode bind param value '/tmp/testunique'
param[key.encode(encoding)] = processors[key](compiled_params[key])
/home/nicola/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.5.0rc4-
py2.6.egg/sqlalchemy/engine/default.py:241: SAWarning: Unicode type
received non-unicode bind param value 'testunique'
param[key.encode(encoding)] = processors[key](compiled_params[key])
09:15:28,185 INFO [sqlalchemy.engine.base.Engine.0x...c26c] INSERT
INTO samba_shares (id, nome, path, creato, modificato) VALUES (%(id)s,
%(nome)s, %(path)s, %(creato)s, %(modificato)s)
09:15:28,186 INFO [sqlalchemy.engine.base.Engine.0x...c26c]
{'creato': datetime.datetime(2009, 1, 12, 9, 14, 16, 990288), 'path':
'/tmp/testunique', 'nome': 'testunique', 'id': 24L, 'modificato':
datetime.datetime(2009, 1, 12, 9, 14, 16, 990375)}
09:15:28,191 INFO [sqlalchemy.engine.base.Engine.0x...c26c] COMMIT
exit()
all ok the change is commited to database,
now I create another object with the same 'nome', 'nome' has an unique
costraint:
paster shell
from sambadm.model.smbserver import *
import transaction
s1=SambaShares(nome='testunique',path='/tmp/test')
DBSession.add(s1)
try:
transaction.commit()
except:
print 'error'
09:22:11,844 INFO [sqlalchemy.engine.base.Engine.0x...a26c] BEGIN
09:22:11,847 INFO [sqlalchemy.engine.base.Engine.0x...a26c] select
nextval('"samba_shares_id_seq"')
09:22:11,847 INFO [sqlalchemy.engine.base.Engine.0x...a26c] None
/home/nicola/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.5.0rc4-
py2.6.egg/sqlalchemy/engine/default.py:241: SAWarning: Unicode type
received non-unicode bind param value '/tmp/test'
param[key.encode(encoding)] = processors[key](compiled_params[key])
/home/nicola/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.5.0rc4-
py2.6.egg/sqlalchemy/engine/default.py:241: SAWarning: Unicode type
received non-unicode bind param value 'testunique'
param[key.encode(encoding)] = processors[key](compiled_params[key])
09:22:11,851 INFO [sqlalchemy.engine.base.Engine.0x...a26c] INSERT
INTO samba_shares (id, nome, path, creato, modificato) VALUES (%(id)s,
%(nome)s, %(path)s, %(creato)s, %(modificato)s)
09:22:11,851 INFO [sqlalchemy.engine.base.Engine.0x...a26c]
{'creato': datetime.datetime(2009, 1, 12, 9, 21, 49, 29417), 'path': '/
tmp/test', 'nome': 'testunique', 'id': 26L, 'modificato':
datetime.datetime(2009, 1, 12, 9, 21, 49, 29527)}
09:22:11,855 INFO [sqlalchemy.engine.base.Engine.0x...a26c] ROLLBACK
09:22:11,856 ERROR [txn.-1210808640] Error in tpc_abort() on manager
<zope.sqlalchemy.datamanager.SessionDataManager object at 0x9356c4c>
Traceback (most recent call last):
File "/home/nicola/tg2env/lib/python2.6/site-packages/
transaction-1.0a1-py2.6.egg/transaction/_transaction.py", line 454, in
_cleanup
rm.tpc_abort(self)
File "/home/nicola/tg2env/lib/python2.6/site-packages/
zope.sqlalchemy-0.3-py2.6.egg/zope/sqlalchemy/datamanager.py", line
82, in tpc_abort
raise TypeError("Already committed")
TypeError: Already committed
error
this in a web request give an "internal server error" even if I used
try except and even if the error is catched and the catch statment is
executed (print 'error' in this case),
so the only solution is to make a query before the commit
something like this:
esiste=DBSession.query(SambaShares).filter
(SambaShares.nome=='testunique').first()
if not esiste:
add to session and then commit
else:
give an error to users
thanks
drakkan
On 11 Gen, 23:45, "Jorge Vargas" <[email protected]> wrote:
> On Sun, Jan 11, 2009 at 4:23 PM, Diez B. Roggisch <[email protected]> wrote:
>
> >>> I need to make a query before saving to check if the unique costraint
> >>> is violated? In my opinion a best solution is a way to trap the
> >>> dberror and show an error message to the users
>
> >> no you need to "flush to the db" in tg2 this is done by issuing
> >> transaction.commit(), which together with other things calls
> >> DBSession.flush()
>
> > This is not correct. At least the "no you need" (which I presume you
> > mean "you don't need") part. Because of the lazyness of SQLAlchemy, a
> > mapped object won't get an ID until the next flush. As you correctly
> > state, this is eventually done implicitly by the commit.
>
> actually it was supposed to read 'no, you need to "flush to the db" ',
> that will trigger the validation of the unique constraint, I'm not
> sure why you think the id is involved. The first question "I need to
> make a query...." is a common misconception, of the lazyness of SA. In
> other words, you need to call transaction.commit() to get the object
> into the db (flush), but you can alternatively get this done, by
> issuing a query which causes the session to flush, in order to keep
> data consistent.
>
> > But often enough when you create an object, you want to e.g. render a
> > page containing it's ID - and thus you need to call flush.
>
> > Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---