how to migrate django models to new database
Question:
i develope a django project on my local system, and now i deployed that to a server.
i used MySql as database. whenever i try to migrate my models (using python manage.py migrate
or python manage.py makemigrations
) to server’s new database, it shows me following error:
django.db.utils.ProgrammingError: (1146, "Table '<DB_NAME>.mainapp_service' doesn't exist")
and doesn’t create tables into the database.
i didn’t move any of my local migration
files to server, just moved mainapp/migrations/__init__.py
file.
how can i create all tables and run my app?
Answers:
You really shouldn’t run makemigrations
on the server. You should include migrations in version control and push your migrations files to the server and then run migrate
. Make sure you have created the database on the server and that you have it confugured in settings.py. Check out the Workflow section on migrations.
Configuration of MySQL in Django
settings.py
import pymysql # MySQL
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # db engine
'NAME': 'mydb', # db name
'USER': 'root', # db username
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '3306',
}
}
DB migrations
Python3 does NOT support MySQLdb,pymysql
is an alternative.
1.First,install pymysql
in venv
pymysql:pip install pymysql
2.Add the following codes in _init_.py
or settings.py
.
import pymysql
pymysql.install_as_MySQLdb()
- Run the migration command in the Terminal:
python manage.py makemigrations
python manage.py migrate
Note:If it shows No changes detected
after python manage.py makemigrations
is runned, then try to run python manage.py makemigrations --empty appname
.
Data migration
Export SQLite data into MySQL
1.SQLite data export
settins.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
In the terminal
python manage.py dumpdata > data.json
Export data to the data.json
file in the root dictionary。
2.Import data to MySQL
Modify the configuration from Django to MySQL in the settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'IP',
'PORT': '3306',
}
}
run
python manage.py loaddata data.json
Note:
Make sure the Mysql tables are null.
Make sure that the Mysql user table
is empty. If you have migrated data to Mysql before, you may get an error. Pay attention to the error message that is reported when there is an error. If you are prompted for duplicate primary keys, you need to delete the data first. This data is generated when applying migration files to the MySQL database, usually the tables related to content_type
.
In the MySQL terminal:
use db_name;
delete from auth_permission;
delete from django_content_type;
If it errors, delete all data and imort the db again。Basically, the failure to import data is caused by the existence of data in MySQL.
3.Load the time zone table
In addition, it is possible that the installed MySQL does not load the time zone table. The official Django documentation also points out this problem, and the MySQL website also has a corresponding method to deal with it: load the time zone table.
Linux/Mac
The solution is very simple. windows system should first download a sql file:timezone_2018e_posix_sql.zip
After downloading, unzip it to get a sql file, then execute the cmd command to import the file:
mysql -u root -p mysql < timezone_posix.sql
4. MySQL to PostgreSQL
python manage.py dumpdata > backup.json
In PostgreSQL Configuration:
python manage.py loaddata backup.json
Database migration between different APPs
1.Generate model files
python3 manage.py inspectdb
2.Importing model files into the app
Create an app
python3 manage.py startapp 'app_name'
3.Import the model into the created app
python3 manage.py inspectdb > app/models.py
i develope a django project on my local system, and now i deployed that to a server.
i used MySql as database. whenever i try to migrate my models (using python manage.py migrate
or python manage.py makemigrations
) to server’s new database, it shows me following error:
django.db.utils.ProgrammingError: (1146, "Table '<DB_NAME>.mainapp_service' doesn't exist")
and doesn’t create tables into the database.
i didn’t move any of my local migration
files to server, just moved mainapp/migrations/__init__.py
file.
how can i create all tables and run my app?
You really shouldn’t run makemigrations
on the server. You should include migrations in version control and push your migrations files to the server and then run migrate
. Make sure you have created the database on the server and that you have it confugured in settings.py. Check out the Workflow section on migrations.
Configuration of MySQL in Django
settings.py
import pymysql # MySQL
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # db engine
'NAME': 'mydb', # db name
'USER': 'root', # db username
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '3306',
}
}
DB migrations
Python3 does NOT support MySQLdb,pymysql
is an alternative.
1.First,install pymysql
in venv
pymysql:pip install pymysql
2.Add the following codes in _init_.py
or settings.py
.
import pymysql
pymysql.install_as_MySQLdb()
- Run the migration command in the Terminal:
python manage.py makemigrations
python manage.py migrate
Note:If it shows No changes detected
after python manage.py makemigrations
is runned, then try to run python manage.py makemigrations --empty appname
.
Data migration
Export SQLite data into MySQL
1.SQLite data export
settins.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
In the terminal
python manage.py dumpdata > data.json
Export data to the data.json
file in the root dictionary。
2.Import data to MySQL
Modify the configuration from Django to MySQL in the settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'IP',
'PORT': '3306',
}
}
run
python manage.py loaddata data.json
Note:
Make sure the Mysql tables are null.
Make sure that the Mysql user table
is empty. If you have migrated data to Mysql before, you may get an error. Pay attention to the error message that is reported when there is an error. If you are prompted for duplicate primary keys, you need to delete the data first. This data is generated when applying migration files to the MySQL database, usually the tables related to content_type
.
In the MySQL terminal:
use db_name;
delete from auth_permission;
delete from django_content_type;
If it errors, delete all data and imort the db again。Basically, the failure to import data is caused by the existence of data in MySQL.
3.Load the time zone table
In addition, it is possible that the installed MySQL does not load the time zone table. The official Django documentation also points out this problem, and the MySQL website also has a corresponding method to deal with it: load the time zone table.
Linux/Mac
The solution is very simple. windows system should first download a sql file:timezone_2018e_posix_sql.zip
After downloading, unzip it to get a sql file, then execute the cmd command to import the file:
mysql -u root -p mysql < timezone_posix.sql
4. MySQL to PostgreSQL
python manage.py dumpdata > backup.json
In PostgreSQL Configuration:
python manage.py loaddata backup.json
Database migration between different APPs
1.Generate model files
python3 manage.py inspectdb
2.Importing model files into the app
Create an app
python3 manage.py startapp 'app_name'
3.Import the model into the created app
python3 manage.py inspectdb > app/models.py