On Jul 7, 2011, at 12:43 PM, Ben Sizer wrote:

> I have a Column(Integer) called object_id. I assign to it a string or
> unicode value, eg. object_id = u"not an integer". To my surprise, this
> doesn't raise any kind of exception when the row is committed. I can
> then expunge the session and request that row back, getting a Unicode
> object for that column.
> 
> I understand that sqlite is very weakly typed and that you can do this
> sort of thing easily. But I thought that SQLAlchemy would apply some
> logic in the middle to ensure that an Integer column only takes
> something integral. I would understand if I'd passed "30" or some
> other string that could be coerced to an integer, but this doesn't fit
> that constraint.
> 
> So, 2 questions:
> 
> a) Is this expected behaviour?
> b) How can I catch this, ideally at the SQLAlchemy level, so that I
> can't accidentally store a string as an integer?


The types do as little as possible, with the exception of the "assert_unicode" 
feature of String, as well as SQLite's date type since we have to coerce to a 
string (and in the latter case we used to get a lot of requests to let strings 
pass through).

This is a performance-critical point and SQLAlchemy seeks to do as little as 
possible with type coercion, we defer to the DBAPI to determine in most cases 
if a value is not acceptable.

For validation of incoming values, the options are TypeDecorator at the Core 
level and @validates at the ORM level:

http://www.sqlalchemy.org/docs/core/types.html#augmenting-existing-types
http://www.sqlalchemy.org/docs/orm/mapper_config.html#simple-validators

A recipe to apply validators to all occurrences of a type:

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/ValidateAllOccurrencesOfType


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

Reply via email to