Django MySQL error when creating tables

后端 未结 11 1576
感动是毒
感动是毒 2020-12-14 01:02

I am building a django app with a MySQL DB. When I run \'python manage.py migrate\' for the first time, some tables are created well then some errors appear. The error broug

11条回答
  •  被撕碎了的回忆
    2020-12-14 01:41

    I ran into a similar issue while running tests on a Django library against a MySQL database (to avoid Django 2.2 incompatibilities with CentOS 7). This is similar to the problem carton.swing's answer addresses, but takes a different approach to solve the problem.

    A couple of notes about the library I'm testing:

    • The library does not define any models, but the test suite defines models for testing purposes
    • The test suite depends on foreign keys to models it does not define, specifically the auth.User model.

    After looking into an old Django ticket, I noticed this response:

    your problem is that an unmigrated app (authtoken) depends (has a FK to) a migrated app (main). This is a well-known no-no.

    (I couldn't find any other references to this being a "well-known no-no".) Since the test suite defines some models, and also uses auth.User supplied by Django, I guessed that the test runner (pytest, in this case) migrated models defined in the test suite but did not migrate auth.User. To test this, I turned off migrations on my test suite (for pytest-django, this just required adding a --no-migrations flag), which fixed the issue.

提交回复
热议问题