You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to save a Parent instance using BaseChildFormSet, an integrity error occurs if there are two or more Child instances with the same name and parent. This issue specifically arises when utilizing a UniqueConstraint in the Child model, and does not occur when using unique_together.
The integrity error manifests when attempting to save a Parent instance through BaseChildFormSet, and involves Child instances sharing the same name and parent combination. This problem is observed exclusively with the use of UniqueConstraint.
Expected Outcome
The form should display a validation error when trying to create or update multiple Child instances that violate the UniqueConstraint. This validation error ensures that users are notified of the uniqueness requirement and prevents the creation of conflicting data.
Proposed Solution
I belive that this issue can be solved adjusting the validate_unique method in the BaseChildFormSet . By ensuring that include_meta_constraints=True is used when retrieving unique checks, the formset will incorporate all applicable constraints, including those defined at the meta-level (e.g., UniqueConstraint).
Code Adjustment
defvalidate_unique(self):
'''This clean method will check for unique_together condition'''# Collect unique_checks and to run from all the forms.all_unique_checks=set()
all_date_checks=set()
forms_to_delete=self.deleted_formsvalid_forms= [formforforminself.formsifform.is_valid() andformnotinforms_to_delete]
forforminvalid_forms:
unique_checks, date_checks=form.instance._get_unique_checks(include_meta_constraints=True)
all_unique_checks.update(unique_checks)
all_date_checks.update(date_checks)
The text was updated successfully, but these errors were encountered:
giodiasdev
changed the title
BaseChildFormSet doesn't caught IntegrityError for unique toguether using UniqueConstraint
BaseChildFormSet doesn't caught IntegrityError for unique together using UniqueConstraint
Jul 24, 2024
Issue Description
Problem Statement
When attempting to save a
Parent
instance usingBaseChildFormSet
, an integrity error occurs if there are two or moreChild
instances with the samename
andparent
. This issue specifically arises when utilizing aUniqueConstraint
in theChild
model, and does not occur when usingunique_together
.Context
The models involved are structured as follows:
Issue Specifics
The integrity error manifests when attempting to save a
Parent
instance throughBaseChildFormSet
, and involvesChild
instances sharing the samename
andparent
combination. This problem is observed exclusively with the use ofUniqueConstraint
.Expected Outcome
The form should display a validation error when trying to create or update multiple Child instances that violate the UniqueConstraint. This validation error ensures that users are notified of the uniqueness requirement and prevents the creation of conflicting data.
Proposed Solution
I belive that this issue can be solved adjusting the
validate_unique
method in the BaseChildFormSet . By ensuring thatinclude_meta_constraints=True
is used when retrieving unique checks, the formset will incorporate all applicable constraints, including those defined at the meta-level (e.g.,UniqueConstraint
).Code Adjustment
The text was updated successfully, but these errors were encountered: