Rapsey
Rapsey

Reputation: 589

Web2py: id as foreign key

I am trying to implement some tables that mimic inheritance in a relational (sqlite) database. My goal is to have one master table with general fields, and some child tables with specialized fields. Every record in the master table will have exactly one counterpart in exactly one of the specific tables. What I want looks like this:

Master table:

id (PK) #Your average web2py unique auto-incrementing int field
#Some general fields

Child tables:

id (PK, FK referencing the PK of the master table)
#Some specialized fields

Having a default "id" PK for each child table has no use to me. All I need is an FK reference to the master table, which can serve as the PK for the child tables. This means that the PK of the child tables will be unique, but will contain gaps. This is because a child table will only reference some of the records of the master table.

I get the impression that doing this (not giving the child tables the regular "id" PK) is against the Web2py way. So my questions are as follows:

  1. Is this a Bad Idea™? If so, why?
  2. How would I implement this? Can Web2py handle tables that don't have an auto-incrementing int as PK?

It's very important for me to apply the correct style and practice when writing code. I am open to alternative approaches.

Thank you for your help.

Upvotes: 0

Views: 1432

Answers (1)

Anthony
Anthony

Reputation: 25536

See the book section on keyed tables. If this is a database specifically for use with web2py, I suggest you stick with the standard web2py approach:

db.define_table('master', ...)

db.define_table('child', Field('master', db.master))

Upvotes: 1

Related Questions