Non negative integer column

323 views
Skip to first unread message

Avishay Balderman

unread,
Jun 9, 2013, 8:14:21 AM6/9/13
to sqlal...@googlegroups.com
Hi
I would like to create a table with an integer column.
This column value must be >= 0.
How can I enforce it using table defintion?
i have looked here and it looks fine.
Will it work against any DB?
My backing DB is MySQL.

Thanks

Avishay

Rory Hart

unread,
Jun 9, 2013, 8:07:04 PM6/9/13
to sqlal...@googlegroups.com
from sqlalchemy.dialects.mysql import INTEGER as Integer 
my_int = Column(Integer(unsigned=True))


Keep in mind, as Theron notes on his blog post, that this is MySQL specific so you won't have compatibility with other DBs.



Avishay

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, 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.
 
 

Michael Bayer

unread,
Jun 9, 2013, 8:08:09 PM6/9/13
to sqlal...@googlegroups.com
have you considered just using an UNSIGNED INT ?  MySQL offers support for such types.

The CHECK constraint is a good way to go as well, but unfortunately I believe MySQL does not actually enforce CHECK constraints (funny, huh?) .  Here's an SO question regarding that issue: http://stackoverflow.com/questions/5807231/mysql-check-constraint-alternative  .




Avishay Balderman

unread,
Jun 10, 2013, 3:19:21 AM6/10/13
to sqlal...@googlegroups.com
Hi
I am looking for a portable solution - A solution that will work when I switch from DB A to DB B.
so I do not want to include any MySQL specific code.
CheckConstraint looks promising.. but you mentioned it is not supported by MySQL.

Any other ideas?

Thanks

Avishay

Avishay Balderman

unread,
Jun 10, 2013, 4:30:15 AM6/10/13
to sqlal...@googlegroups.com
How about using UserDefinedType  http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#sqlalchemy.types.UserDefinedType and declare a type 'NonNegativeInteger'.
Will it be enough to protect the table from negative integers?

Michael Bayer

unread,
Jun 10, 2013, 9:46:54 AM6/10/13
to sqlal...@googlegroups.com, sqlal...@googlegroups.com
Oh.  Well a python side rule is very different from a server side rule, but if app side is all you need then sure you have a lot of options there.   Use a TypeDecorator, check the docs there are many examples.

Sent from my iPhone

Charlie Clark

unread,
Jun 10, 2013, 10:33:25 AM6/10/13
to sqlal...@googlegroups.com
Am 10.06.2013, 15:46 Uhr, schrieb Michael Bayer <mik...@zzzcomputing.com>:

> Oh. Well a python side rule is very different from a server side rule,
> but if app side is all you need then sure you have a lot of options
> there. Use a TypeDecorator, check the docs there are many examples.

FWIW David Mertz has just written an interesting checker for Python 3

http://code.activestate.com/recipes/578528-type-checking-using-python-3x-annotations/

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Avishay Balderman

unread,
Jun 11, 2013, 3:00:37 AM6/11/13
to sqlal...@googlegroups.com
I prefer to have a server side solution... but cant find a generic one.
Reply all
Reply to author
Forward
0 new messages