I wonder how you build your models, do they handle all the data logic?

Porting myself to Python/Pylons, I have to learn to use SQLAlchemy
properly because looking through the 300+ pages of its documentation it
seems complex but very powerful, especially its ORM.

However in my PHP apps I always contain all the data logic to the model
itself. The controllers only call specific methods in order to do
something with the model, not the data directly: loadById() would load
model data into its properties from relevant tables. save() would check
if pkey is set, and then insert or update existing rows accordingly.

Also, since I work exclusively with transactional Postgres, I like to
rely on constraints to tell me  what's wrong. Instead of locking entire
table to see if new data would produce duplicates in columns that should
be unique, I rely on the DB transaction to raise Constraint Violation
error from which I read the column in question and can inform the user
to modify this or that field in the form, because it's duplicate.

The problem with that is that the DB engine throws a generic Constraint
Violation from which I have to "extract" constraint name, and produce a
meaningful message to the user, like "Name already exists, please try
another". Therefore I catch generic SQL exceptions in the model save()
method, and re-throw custom exceptions made for the model like, for
example, NameDuplicateError, or EmailDuplicateError, etc... so the
controller can catch these and send back proper message, or alter the
form to highlight fields in question.

I wonder what other patterns would you guys use?



Vlad

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en.

Reply via email to