ken.riel a écrit :
> Hello,
>
> I have a problem catching a sqlalchemy error in a try and expect.
>
> You see in the model that my user_name must be unique. So if the is a
> user_name like "Ken" and i fill in my form the name "Ken" for user_name
> i will get an error like this:
>
> SQLError: (IntegrityError) column user_name is not unique
>
> So i like to catch the SQLError in the expect.
>
> Model code:
> users_table = Table ('users', metadata,
> Column('user_id', Integer, primary_key=True),
> Column('user_name', Unicode(16), unique=True),
> Column('user_lastname', Unicode(255)),
> Column('user_zipcode', Unicode(6)),
> Column('display_name', Unicode(255),),
> Column('password', Unicode(40)),
> Column('created', Date, default=date.today)
> )
>
> class Users(object):
> pass
>
> assign_mapper(session.context, Users, users_table)
>
>
> Controller code:
> @expose()
> def drop_user(self, user_id=None, name="", last_name="", zipcode="",
> password="", msg=""):
> try:
> user = Users(user_name=name, user_lastname=last_name,
> user_zipcode=zipcode, password=password)
> except ??? :
> msg = "The username is already in the database"
> redirect ("/add_user", msg=msg)
>
> What do i put by the ??? to except the SQLError for sqlalchemy
>
>
As far as I understand, the problem is that the actual SQL is executed
"outside" of the scope of drop_user, not when you do the user = Users(...)
What you can do is checking that the user does not already exist before
trying to create a new one.
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---