Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint error after a validation from model #215

Closed
alepn opened this issue May 13, 2021 · 3 comments
Closed

Constraint error after a validation from model #215

alepn opened this issue May 13, 2021 · 3 comments

Comments

@alepn
Copy link

alepn commented May 13, 2021

After a validation from model (ex.: unique field violate), I cannot send the request again.

It causes a constraint error:

null value in column "foo_id" violates not-null constraint

@yezyilomo
Copy link
Owner

It’s hard to know what might be causing that problem if you don’t post a piece of code which you think it might be causing that error, so please do.

@alepn
Copy link
Author

alepn commented May 14, 2021

MODELS

class Profile(models.Model):
    name = models.CharField('Name', max_length=80)

class Email(models.Model):
    profile = models.ForeignKey(Profile)
    email = models.CharField('Email', max_length=80, unique=True)

SERIALIZERS

from django_restql.mixins import DynamicFieldsMixin
from django_restql.serializers import NestedModelSerializer

class EmailSerializer(DynamicFieldsMixin, NestedModelSerializer):
    class Meta:
        model = Email
        fields = ['profile','email']

class ProfileSerializer(DynamicFieldsMixin, NestedModelSerializer):
    email_set = NestedField(EmailSerializer, many=True, required=False, allow_null=True)
    class Meta:
        model = Email
        fields = ['name','email_set']

If you send a request with a existing email, it will fail because the unique constraint.
If you resend the request with another e-mail, it show a fk constraint error, because profile_id on nested email_set was lost.
Tested with django-restql versions: 0.10.2 and 0.11.2

@yezyilomo
Copy link
Owner

This is not django-restql's problem, DRF doesn't handle all IntegrityErrors raised by django ORM due to failed database constrains, so you are supposed to handle these yourself by using fields validation on your serializers, the way you did it here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants