Note, when you create a table with define_table, it automatically creates a
field called "id" of type "id", which serves as the record ID. In your
test_counts table, the test_id field is therefore referencing the "id"
field of the tests table, not the test_id field, which is just a regular
integer field. In any case, because of the ondelete='CASCADE', when you
delete a record from tab.tests, it should delete any records from
tab.test_counts that reference that record.
Anthony
On Sunday, March 4, 2012 12:42:05 PM UTC-5, Jan Rozhon wrote:
>
> Hi all, I have a newbie question regarding the cascade delete of
> referenced database entries. Basically, I have these two tables:
> tab = DAL('sqlite://tab.sqlite')
> tab.define_table('tests',
> Field('test_id', 'integer', required=True, default=''),
> Field('test_pid', 'integer', ondelete='NO ACTION'),
> Field('args'))
> tab.define_table('test_counts',
> Field('test_id', tab.tests, requires=IS_IN_DB(tab,
> tab.tests.test_id, '%(test_id)s'), ondelete='CASCADE'),
> Field('message'))
>
> and I want web2py to create SQLite tables tests and test_counts.
> Test_counts should reference "test_id" field from the tests table and
> whenever the test entry with the corresponding test_id is deleted the
> appropriate rows in test_counts should be deleted as well, but it doesnt
> work that way and I am unable to find a solution either using my knowledge
> or google as well.
>
> Thanks in advance for any answer, Jan
>