Need help setting up a composite key

131 views
Skip to first unread message

Mark Hayes

unread,
Feb 7, 2011, 2:43:34 AM2/7/11
to web2py-users
I'm new to web2py and can't seem to find out how to create a composite
key. Specifically I'm trying to create the following tables:

db.define_table('volunteer',
Field('first_name', 'string', length=30, notnull=True),
Field('last_name', 'string', length=30, notnull=True),
Field('birthday', 'date', required=True),
Field('email', 'string', length=30))
db.define_table('opportunity',
Field('name', 'string', length=30, notnull=True))
db.define_table('interest',
Field('volunteer', db.volunteer),
Field('opportunity', db.opportunity))

In the 'interest' table I want to ensure that the combination of both
volunteer + opportunity is unique. What is the best way to go about
doing this? Any help would be greatly appreciated, thanks!

dederocks

unread,
Feb 7, 2011, 8:35:30 AM2/7/11
to web2py-users
Hi!
Check this thread:
http://groups.google.com/group/web2py/browse_thread/thread/caaf68de3fbc0baf/b9812b04ac70ba8b?lnk=gst&q=Multiple+constraint#b9812b04ac70ba8b

In summary, you want to create a computed combined field, and require
that it is unique. A bit odd, but that's what you need to be able to
have validators working at the DAL level. Otherwise, you can of course
add SQL-level combined indexes (but specific to the database
selected), which if not respected will generate an error instead of a
friendly validator message.
BR, Andre

Mark H

unread,
Feb 8, 2011, 12:44:50 AM2/8/11
to web...@googlegroups.com
Hey André,

Thanks, this is just what I was looking for.  It's a little bit odd to have to go about doing this with a computed field, but I think I can live with that :)
Reply all
Reply to author
Forward
0 new messages