duplicate key value violates unique constraint Error using sql_constraints - odoo

I have used as
_sql_constraints = [
('bpv_uniq', 'unique (branch_id,product_id,product_tmpl_id)', "There are Other Reference Purchase Price in same branch, please change branch"),
]
I have removed this code and upgrade module. But on data entry still gives the following error;
duplicate key value violates unique constraint "reference_price_uniq"
DETAIL: Key (branch_id,product_id,product_tmpl_id)=(2,31,27) already exists.
Please guide.

One way is to override the constraint to check nothing instead.
_sql_constraints = [('bpv_uniq', 'CHECK(1==1)', "There are Other Reference Purchase Price in same branch, please change branch"),]

Related

Odoo 12: Many2one ondelete message?

Is it possible to change(edit) default ondelete message in Many2one field?
My field is:
parent_id = fields.Many2one("pgp.organizational.classifications", string="Parent classification", select=True, ondelete='restrict')
Default message is like this, but I won't to add my message:
"Odoo Server Error - Greška kod provjere
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[objekt s referencom: pgp.organizational.classifications - pgp.organizational.classifications] "
You cannot change it in the Many2one field's declaration.
Code which generates this message is there: https://github.com/odoo/odoo/blob/12.0/odoo/service/model.py#L120-L154
Seems to be tricky to overload
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.
I solved it by overloading unlink method.
Here is the code if it helps someone:
> #api.multi
> def unlink(self):
> for field in self:
> if field.parent_id:
> raise UserError(_('It is not possible to delete a record that is already used in transactions!'))
> return super(YourClass, self).unlink()

Remove SQL constraint in OpenERP7

In OpenERP7, the core module account has a declaration for account.invoice which has, at some point, the following declaration:
addons/account/account_invoice.py:343
_sql_constraints = [
('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'),
]
In a module which redefined account.invoice I wanted to remove the constraint with two different approaches:
Removing it in init (account_invoice::init(self, pool, cr))
def __init__(self, pool, cr):
super(account_invoice, self).__init__(pool, cr)
try:
cr.execute('ALTER TABLE account_invoice DROP CONSTRAINT IF EXISTS account_invoice_number_uniq')
finally:
pass
Replacing the constraint
_sql_constraints = [
('number_uniq', 'check(1=1)', 'Dummy check, always true, used to replace the previous constraint'),
]
However, when I reinstall the module in which those two declarations were made, I get an error (in the PG logs) telling me that the constraint account_invoice_number_uniq could not be craeted for a unique key since there's repeated data.
How can I prevent having such error? How can I prevent the system attempting to create (first; then... replace/delete) the constraint?
Check the Below Reference Link to remove the SQL Constraint Of Parent Class in Odoo
Click To See the Reference For Remove the SQL Constraint In Odoo(formally OpenERP)

Adding a new entry that has no foreign keys results in a "The relationship could not be changed [..]" error

I am running into a weird problem. When I deploy my code, everything works. I can add orders to my system. However, after some orders has been made, I start to get the famous error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Now, I have read a lot about that error, but it doesn't help my case. I have a very simple database without any foreign keys:
My code looks like the following:
public NewOrder Create(NewOrder order)
{
var o = new WebshopOrder();
o.AdvertId = order.AdvertId;
o.DateCreated = DateTime.Now;
o.IsActivated = order.OrderState > OrderState.Created;
o.ProductId = order.Product.Id;
o.UserId = order.UserId.ToString();
Db.WebshopOrder.Add(o);
Db.SaveChanges();
return new NewOrder() { WebshopOrderId = o.Id};
}
Which in turn, then fails when I say Db.SaveChanges().
Any idea what on earth could be wrong here?
I see three possibilities at the moment:
ProductId is being sent to the DB as NULL
OrderId is being sent to the DB as NULL
Timestamp is being sent to the DB as NULL
I think it is complaining about one of those three columns that are the only ones that do NOT allow NULL. I think ProductId has the greatest chance of being the culprit, where your code is being called sometimes with a value there, and others times it is receiving a NULL product.
This may, or may not be the issue, but your code has:
order.Product.Id
Did you want to use this instead?
order.ProductId
That error message would be improved if it were split into two or more errors with each being more specific.
I can see from your question that you are looking into the Order table for relationships.
Instead you have to look into WebshopOrder table for any relations, because you are saving the WebshopOrder
Db.WebshopOrder.Add(o);
Db.SaveChanges();

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
difference between RESTRICT and NO ACTION.
how SET DEFAULT is used in OpenERP 7?
where to set default value for the field ?
how to define set default value in python code itself?
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',
),

SimpleRoleProvider custom defined "UserID" foreign key constraint, possible?

I'm following this blog post to learn the new SimpleMemberProvider and SimpleRoleProvider features in latest release of asp.net MVC.
Instead of calling "UserId", I named it just "Id". Then, however, when I enabled the "SimpleRoleProvider" in web.config, I got error like "Foreign key 'fk_UserId' references invalid table 'MemberProfile'." it seems by default it's looking for UserId column to create foreign key constraint.
Is it possible to user something else rather than "UserId"?
I believe it gets it's name from it's initialization
Look for Filters
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);