I don't understand what you are trying to achieve, but whatever it is, you
are doing it wrong; your model should be:
db.define_table('A', Field('name'))
db.define_table('B', Field('name'), Field('id_from_table_a', 'reference A'))
# alternatively:
# db.define_table('B', Field('name'), Field('id_from_table_a', db.A))
This would create the foreign key reference, and would allow nulls by
default (which is what your condition does).
But as I said, I'm not sure exactly what you are trying to achieve?
On Wednesday, April 18, 2012 7:11:42 PM UTC-4, Cliff wrote:
>
> There are two tables, as follows:
>
> db.define_table('A', Field('name'))
>
> db.define_table('B', Field('name'), Field('id_from_table_a))
>
> Also there are two applications. One, called 'both_a_and_b', uses both
> tables and uses 'table_a_id' as a foreign key in table B. The other,
> called 'table_b_only' needs CRUD access to the information in table B, but
> it is not able to supply a value for 'id_from_table_a.'
>
> I think because Postgres recognizes foreign key constraints,
> 'table_b_only' will not be able to create records in table B.
>
> What Is the right solution?
>
> I can think of two. First, create a third table, C, for all the data that
> 'table_b_only' needs. This table would not have the 'id_from_table_a'
> field. The other application would need to write also to this table
> whenever it creates a record in table B.
>
> A second possibility might be to define table B this way:
> db.define_table('B',
> Field('name') ,
> Field('id_from_table_a', requires= IS_EMPTY_OR(IS_IN_DB(db, 'A.id',
> ...)))
> )
>
> I would be grateful for any guidance,
> Cliff Kachinske
>