Here the error
<class 'psycopg2.IntegrityError'> ERREUR: UPDATE ou DELETE sur la table « table2 » viole la contrainte de clé étrangère « table3_field2t3_fkey » de la table « table3 » DETAIL: La clé (id)=(1) est toujours référencée à partir de la table « table3 ».
Here the code :
# Model
db.define_table('table1',Field('fieldt1','string'), format='%(fieldt1)s')
db.define_table('table2',Field('fieldt2','string'), Field('field2t2', 'reference table1', ondelete='NO ACTION'), format='%(fieldt2)s')
db.define_table('table3',Field('fieldt3','string'), Field('field2t3', 'reference table2', ondelete='NO ACTION'), format='%(fieldt3)s')
# Controller
def create_update():
"""create update funciton"""
form = crud.update(db[request.args(0)], request.args(1))
return dict(form=form)
I attach the app...
The problem arrive when I try to delete a record in table2 that is attached to a record in table 3.
I can make a function that will check if this record is attached by a record in table 3 before trying to deleted it, but I am curious to know if there is an other solution.
The database is Postgres.
I just test with SQLite and it is even worse, deletion of the referenced record is allowed but the record in the table 3 still reference the record. Form my point of view deletion of a record referenced shouldn't be allowed if there is a ondelete clause apply to the reference field. I also try to change the 'NO ACTION' for 'CASCADE' and if I delete a record of table 3 that reference table2 record the referenced table 2 record doesn't get deleted (still with SQLite).
I use web2py 2.2.1 for all these test.
Hope my model is correct.
I just test also with Postgres (8.4) and even when CASCADE is setted the referenced records are not deleted on deletion.
Thanks
Richard