Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

6

ondelete optional parameter

By
Atchuthan
on 5/31/13, 4:08 AM 8,738 views
Hi guyz,
    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 tell me when these values are set at what scenario and give me example for each predefined values in detail.

For eg: 
"restrict" - 
'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='restrict', select=True),
- journal values must not be changed.
7

Vasiliy Birukov

--Vasiliy Birukov--
3446
| 6 6 8
Russian Federation
--Vasiliy Birukov--

Help develop and implement any project on base Odoo.

Vasiliy Birukov
On 6/3/13, 2:36 AM

It is Foreign Key constraint on delete records in your object. These parameters define constraint on PostgreSQL level.

Restricting and cascading deletes are the two most common options. RESTRICT prevents deletion of a referenced row. NO ACTION means that if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything. (The essential difference between these two choices is that NO ACTION allows the check to be deferred until later in the transaction, whereas RESTRICT does not.) CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well. There are two other options: SET NULL and SET DEFAULT. These cause the referencing columns to be set to nulls or default values, respectively, when the referenced row is deleted. Note that these do not excuse you from observing any constraints. For example, if an action specifies SET DEFAULT but the default value would not satisfy the foreign key, the operation will fail.

can you explain about RESTRICT and NO ACTION in detail

For SET DEFAULT: 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='set default', select=True),

So how to set the default value when deleted.

Atchuthan
on 6/3/13, 2:53 AM

Behavior RESTRICT and NO ACTION through use in framework will the same. It affect moment of check constraint and can control through direct SQL queries not through framework. The same for SET DEFAULT, you can't define default values through framework. If you need, you can read how it do more details in PostgreSQL documentation.

Vasiliy Birukov
on 6/3/13, 5:37 AM

can you give me an example for SET DEFAULT. Tell me the actions to take for setting the value for a field in PostgreSQL.

Atchuthan
on 6/3/13, 5:48 AM

Example from PostgreSQL documentation: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77

Vasiliy Birukov
on 6/3/13, 7:34 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/31/13, 4:08 AM
Seen: 8738 times
Last updated: 6/27/16, 1:04 AM