Forums

Migrate existing database fields.E340 error on run

Hey everyone I'm fairly new to Python Anywhere, I'm running a django app on python 3.7, I've manually added the tables and fields I need via SSH in MySQL Workbench and ran [migrate.py inpectdb > /app/models.py] then makemigrations then when I run Migrate I get this:

ERRORS: auth.Group.permissions: (fields.E340) The field's intermediary table 'auth_group_permissions' clashes with the table name of 'app.AuthGroupPermissions'. auth.User.groups: (fields.E340) The field's intermediary table 'auth_user_groups' clashes with the table name of 'app.AuthUserGroups'. auth.User.user_permissions: (fields.E340) The field's intermediary table 'auth_user_user_permissions' clashes with the table name of 'app.AuthUserUserPermissions'.

If I remove the auth tables from the models.py and try to migrate I get:

File "/usr/lib/python3.7/site-packages/MySQLdb/connections.py", line 276, in query _mysql.connection.query(self, query) django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

From what I've read it is conflicting with the settings.py [INSTALLED_APPS] but I'm not sure where to go from here to get the migration to work properly.

Hmm -- that sounds like a kind of strange setup, you wouldn't normally set up tables with workbench and then reverse-engineer the models out of them. I see from your Stack Overflow post that you have your reasons, though.

Does the initial data structure that you're trying to replicate come from some Django site running somewhere else? If so, perhaps you could makemigrations there, and store those migration files, then copy them over to PythonAnywhere and migrate from a clean database.

Hey Giles, thank you for your response, what I did was create a fresh DB in Python Anywhere and then using the MySQL Workbench I copied the data from the legacy SQL database into this one. What confused me was that a bunch of models came into my models.py that I did not add(auth) after the "manage.py inspectdb", it seemed to come from the default fresh python anywhere db, along with the tables I added, but I could be wrong and maybe MySQL Workbench caused those by default?

So I thought about removing the database and keeping the models I made in the models.py and then making a new PA DB and trying to migrate it to the database that way but then I would have to re enter the field values again to the tables.. which is what I wanted the MySQL Workbench for because using console is obnoxious.

I'm fairly new to all of this so if any of my logic is faulty certainly let me know. prior to being uploaded to PA my django app was using the build in db on vs2019.

The extra tables that you noticed were created by Django because you have the app that creates those tables enabled in your INSTALLED_APPS. From the table names involved, I'm guessing it's django.contrib.auth that's adding them. There are probably other tables that are being created that way, but they are just not clashing with the tables you've already created.

The second error you're getting is because you have tried to create a key on a column (or columns) that is too big to be a key. That may still be as a result of the auth_ tables clashing. For instance, the Django model may be specifying a key on the id of a table, expecting it to be an integer column, but your database has a large string column for id instead.

I suspect that you may continue to have issues as long as you try to get the Django database and your database to be in the same database. Django does, however, support multiple databases so you could put your legacy database in one database and have your Django database in another. That way, they have no way on stepping on each other.