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

turn_off_auto_add() caused Model.objects.create() error #109

Open
jxf1002 opened this issue Dec 16, 2021 · 2 comments · May be fixed by #111
Open

turn_off_auto_add() caused Model.objects.create() error #109

jxf1002 opened this issue Dec 16, 2021 · 2 comments · May be fixed by #111

Comments

@jxf1002
Copy link

jxf1002 commented Dec 16, 2021

This is my test case. After seeder.excute() some models, I cannot create this model instance without set created_at field, because turn_off_auto_add update filed attr but not recovery.

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now=False, auto_now_add=True)


class MyTest(TestCase):

    def test(self):
        faker = fake
        seeder = Seeder(faker)
        MyModel.objects.create(name='1')  # no error
        seeder.add_entity(MyModel, 1)
        MyModel.objects.create(name='2')  # no error
        seeder.execute()
        # error: django.db.utils.IntegrityError: NOT NULL constraint failed: django_seed_mymodel.created_at
        MyModel.objects.create(name='3')

code in ModelSeeker

        def turn_off_auto_add(model):
            for field in model._meta.fields:
                if getattr(field, "auto_now", False):
                    field.auto_now = False
                if getattr(field, "auto_now_add", False):
                    field.auto_now_add = False

        manager = self.model.objects.db_manager(using=using)
        turn_off_auto_add(manager.model) # only turn off but not recovery

        faker_data = {
            field: format_field(field_format, inserted_entities)
            for field, field_format in self.field_formatters.items()
        }

        # max length restriction check
        for data_field in faker_data:
            field = self.model._meta.get_field(data_field)

            if field.max_length and isinstance(faker_data[data_field], str):
                faker_data[data_field] = faker_data[data_field][:field.max_length]

        obj = manager.create(**faker_data)

        for field, list in self.many_relations.items():
            list = list(inserted_entities)
            if list:
                for related_obj in list:
                    getattr(obj, field).add(related_obj)

        return obj.pk
@Jaspreet-singh-1032
Copy link

Hey, @jxf1002 I had faced this same problem for which I had opened this #111 pull request. Might be that can fix this issue.

@tongtie
Copy link

tongtie commented Dec 1, 2022

This is my test case. After seeder.excute() some models, I cannot create this model instance without set created_at field, because turn_off_auto_add update filed attr but not recovery.

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now=False, auto_now_add=True)


class MyTest(TestCase):

    def test(self):
        faker = fake
        seeder = Seeder(faker)
        MyModel.objects.create(name='1')  # no error
        seeder.add_entity(MyModel, 1)
        MyModel.objects.create(name='2')  # no error
        seeder.execute()
        # error: django.db.utils.IntegrityError: NOT NULL constraint failed: django_seed_mymodel.created_at
        MyModel.objects.create(name='3')

code in ModelSeeker

        def turn_off_auto_add(model):
            for field in model._meta.fields:
                if getattr(field, "auto_now", False):
                    field.auto_now = False
                if getattr(field, "auto_now_add", False):
                    field.auto_now_add = False

        manager = self.model.objects.db_manager(using=using)
        turn_off_auto_add(manager.model) # only turn off but not recovery

        faker_data = {
            field: format_field(field_format, inserted_entities)
            for field, field_format in self.field_formatters.items()
        }

        # max length restriction check
        for data_field in faker_data:
            field = self.model._meta.get_field(data_field)

            if field.max_length and isinstance(faker_data[data_field], str):
                faker_data[data_field] = faker_data[data_field][:field.max_length]

        obj = manager.create(**faker_data)

        for field, list in self.many_relations.items():
            list = list(inserted_entities)
            if list:
                for related_obj in list:
                    getattr(obj, field).add(related_obj)

        return obj.pk

Thank you, pointed out the wrong place . I solved it by inheriting this class and deleted this code

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

Successfully merging a pull request may close this issue.

3 participants