finding table/column name of unique constraint failure

23 views
Skip to first unread message

lars van gemerden

unread,
May 19, 2013, 4:39:46 AM5/19/13
to sqlal...@googlegroups.com
Hi all,

I generate webpages in which end-users can input data that is stored in a database via sqla.The datamodel is not pre-defined but created by (other) users/designers.   

My question is how can i best let the end-user know which field contains the error if the end-user violates a unique constraint; e.g. if the user creates an account by filling in a username/password, the username must be unique so the user must get an error ("username already exists" oss) if he tries to fill in an existing username.

If i commit the data, the exception thrown is somewhat cryptic (is the message created by sqla or the database?). 

Some related questions:

- are there arguments in the exception i could use?
- should i just query the table for the existence of the unique constrained value (i seem to remember that is impossible in the "validate" solution, so how else)?

I might be able to come up with some sort of solution, but i am looking for a good/the best (structurally sound, not too slow, not overly complex) solution, also useable for other constraints (e.g. non-null, maybe other query/multi-record based constraints). 

Cheers, Lars

Michael Bayer

unread,
May 19, 2013, 6:52:03 PM5/19/13
to sqlal...@googlegroups.com
the messages are generated by the database and the DBAPI in use, SQLAlchemy just passes them through. There's no standardization for those messages, so if your target database isn't providing enough parseable information, you may need to do a quick query against the unique fields on the table first in order to produce validation logic.


lars van Gemerden

unread,
May 21, 2013, 2:41:41 AM5/21/13
to sqlal...@googlegroups.com
Thanks, Michael




--
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/taHEXCvX2p0/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.





--
====================================
Lars van Gemerden
la...@rational-it.com
+31 6 26 88 55 39
====================================
Reply all
Reply to author
Forward
0 new messages