you'd want to put a unique constraint on your username column, then let the 
transaction fail, do a rollback(), report the username isn't available.   If 
you'd like to do the rollback and then continue without losing other pending 
changes, you'd use begin_nested(). 

On Oct 31, 2010, at 3:35 PM, Guguscat wrote:

> Hello,
> 
> I am using SQLAlchemy in a Pylons project and I like it.
> 
> However I am questioning about the best way to insert data into a
> database.
> 
> I have a User class mapping my user table which has a Column
> 'username' which has a unique constraint.
> 
> So I have a form to add a new user when I submit the form I query the
> database to see if the 'username' is already being used.
> 
> I think the way I'm doing it is not good in the case where I have two
> people trying to add the same 'username' at the same time, I guess the
> both queries will return an empty set then when comitting the sessions
> one will go well and one will fail returning a 503 error to the user.
> 
> So I would like instead of doing this try to flush the session and
> catch the exception if any and find the error.
> 
> I don't know which method is the best or if one method is wrong
> furthermore I don't know enough about database systems and if they all
> react the same way.
> 
> Thanks for anyone who could give me more information about this.
> 
> -- 
> 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=en.
> 

-- 
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=en.

Reply via email to