paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
391 stars 67 forks source link

Can't set id if model inherits from non-abstract model #138

Open JanChec opened 3 years ago

JanChec commented 3 years ago

Two kinds of problems here (the cause is probably the same):

class A(models.Model):
    id = models.CharField(max_length=50, primary_key=True)

class B(A):
    pass
Expected :id for b
Actual   :DFKYQIC7NU2NOPVAL2CV5I4JZJPIHA5BP9BHGOWFEX4X825Q5T

    @pytest.mark.django_db
    def test_setting_id():
        from smartpatient.events.models import A, B

        a = N(A, id='id for a')
        assert a.id == 'id for a'
        b = N(B, id='id for b')
>       assert b.id == 'id for b'
E       AssertionError: assert 'DFKYQIC7NU2NOPVAL2CV5I4JZJPIHA5BP9BHGOWFEX4X825Q5T' == 'id for b'

G has the same problem.

If I leave the A.id field default, then:

class A(models.Model):
    pass

class B(A):
    pass
    @pytest.mark.django_db
    def test_setting_id():
        from smartpatient.events.models import A, B

        a = N(A, id=555)
        assert a.id == 555
>       b = N(B, id=666)

(...)

self = <_mysql.connection open to 'mysql-all' at 0x3b65bb8>
query = b'INSERT INTO `events_a` (`id`) VALUES (DEFAULT)'

    def query(self, query):
        # Since _mysql releases GIL while querying, we need immutable buffer.
        if isinstance(query, bytearray):
            query = bytes(query)
>       _mysql.connection.query(self, query)
E       django_dynamic_fixture.ddf.BadDataError: ('events.models.A', IntegrityError(1364, "Field 'id' doesn't have a default value"))

../.virtualenvs/venv3.6/lib/python3.6/site-packages/MySQLdb/connections.py:224: BadDataError

Python 3.6 Django 2.0.13 DDF 2.0.0 (the same problems with 3.1.1)

It worked before the upgrade from Django 1.11.29 and DDF 1.9.5. I suspect the changes in Django broke this functionality in DDF.

Thanks in advance for fixing this! DDF is very, very useful and convenient for testing, generating sample data, and whatnot. Big kudos to all contributors!

JanMalte commented 2 years ago

Hi @Kern3l , is this still an issue with Django 2.2, as this is the only still supported Version of Django 2 (ending April 2022)