atchuthan
atchuthan

Reputation: 181

restrict, no action & set default in ondelete optional parameter for fields

I am learning about optional parameter regarding fields for ondelete parameter. These are the predefined values: "cascade", "set null", "restrict", "no action", "set default"

Can anyone explain in detail about the

Upvotes: 4

Views: 10244

Answers (1)

taper
taper

Reputation: 9824

Take for example a Course with Students. On Students is a foreign key to Course. The ondelete determines what happens with the student_id column (on Course) when the Student is deleted.

  • CASCADE: Delete the Course record with matching student_id when Student is deleted

  • RESTRICT: Cannot delete the Student as long as it is related to a Course.

  • NO ACTION: similar, but is a deferred check: You can delete the Student but you have to make sure that the integrity is OK when the transaction is committed.

  • SET DEFAULT: uses openerp default definition (see _defaults dict in the python model definition)

  • SET NULL: when a Student gets deleted, the student_id becomes NULL in the DB.

In Python you can find these in _columns defintion:

_columns = {
    'student_id': fields.many2one(
        'my.student',
        'Student',
        ondelete='set null',
    ),

Upvotes: 14

Related Questions