0

I am trying to migrate my Django app from SQLite3 to MySql. I took following steps

  1. Configured a new empty database on MySql. I did not create any tables because I assumed that the tables will be created by the migrate command.
  2. Created a new user who has access to this database CREATE USER 'djangouser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';GRANT ALL ON djangoappdb.* TO 'djangouser'@'%';
  3. Stopped the server
  4. Dumped all the data to a file using python3 manage.py dumpdata > currData
  5. Deleted all the existing migrations
  6. reran python3 manage.py makemigrations. This step created a single new migration file
  7. Changed the setting.py file to use MySql and djangoappdb with correct username and password.
  8. ran python3 manage.py migrate command. I get following error
...
File "/usr/local/lib/python3.8/dist-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/local/lib/python3.8/dist-packages/MySQLdb/connections.py", line 259, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'djangoappdb.djangoapp_customuser' doesn't exist")

customuser is one of the models in the app. Am I supposed to create the tables manually?

EDIT2 Here is the customuser model in case it is requested

from django.contrib.auth.models import AbstractUser, Group
# CustomUser:
class CustomUser(AbstractUser):
    pass
    # add additional fields later

    def __str__(self):
        return self.username
user1933205
  • 300
  • 4
  • 12
  • 2
    Are you doing any querying during the initialization of the app registry? See this answer: https://stackoverflow.com/a/39084645/6383431 – ErikR Aug 18 '21 at 20:49
  • I don't think I am doing any querying like that. Also it looks like my error is quite different. It says that the table doesn't exist, which is true. My database is blank and doesn't have any tables in it. I thought the "python3 manage.py migrate" command will create all the tables for me. Isn't it suppose to? – user1933205 Aug 19 '21 at 00:12
  • Where do you load the dumped data? – erik258 Aug 19 '21 at 01:20
  • 2
    ErikR got a good point. Also you should not recreate the migration files. They should work on both prod and dev, even with different databases. If you try to switch back to SQLite, I think it will also fail, that's how I would reassure that the problem is in the migration file. That can happen if they are manually changed/created or maybe the order is wrong – nOperator Aug 19 '21 at 07:00
  • @DanielFarrell: I haven't been able to create the tables yet. Once I get the migrations to work, then I will load the data. – user1933205 Aug 19 '21 at 19:00
  • 1
    https://stackoverflow.com/questions/5164033/export-a-mysql-database-to-sqlite-database explains how to dump sqllite data and load into mysql, table schema included. You shouldn't need to do any migrations operations for a database server migration if you can just dump/load the data – erik258 Aug 19 '21 at 19:50
  • 1
    @ErikR : I revisited your response and rechecked my models. I was indeed querying during the initialization. I had something like this "#auctionWinnerUser = models.ForeignKey('CustomUser', on_delete=models.CASCADE, # default=CustomUser.objects.get(username='NoneSoFar').id)". I have replaced it with "null=True, blank=True". – user1933205 Aug 23 '21 at 00:12

0 Answers0