文章目录
- Django_database migrations 数据库迁移及其相关若干问题/迁移问题排查/迁移和sql语句预览
Django_database migrations 数据库迁移及其相关若干问题/迁移问题排查/迁移和sql语句预览
references
使用Django的个人习惯
常用主命令
-
django-admin and manage.py | Django documentation | Django (djangoproject.com)
-
$ django-admin <command> [options] $ manage.py <command> [options] $ python -m django <command> [options]
-
关于迁移的一些问题参考
-
How to simplify migrations in Django 1.7? - Stack Overflow
-
该问题涉及migaration 故障解决
-
-
I accidentally deleted the migrations folder in Django - Stack Overflow
-
Django deleted migrations directory - Stack Overflow
-
How to Back Up and Restore a MySQL Database {Easy Tutorial} (phoenixnap.com)
误删migrations文件夹后
migration django
sqlmigrate:检查migrations对应的sql操作
Specifies the database for which to generate the SQL. Defaults to default
.
例如
-
python manage.py sqlmigrate polls 0001
-
(ll_env) PS D:\repos\IdeaProjects\djangoProjects\ela_old\ela> py manage.py sqlmigrate word 0001 -- -- Create model Word -- -- -- Create model WordNotes -- -- -- Create model Cet4WordsReq -- CREATE TABLE `cet4_words_req` (`wordOrder` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `spelling` varchar(255) NOT NULL); -- -- Create model Cet6WordsReq --
-
migrations 中的迁移文件
-
migrations.CreateModel( name='Word', fields=[ ('wid', models.AutoField(primary_key=True, serialize=False)), ('spelling', models.CharField(max_length=255)), ('phonetic', models.CharField(blank=True, max_length=255, null=True)), ('plurality', models.CharField(blank=True, max_length=255, null=True)), ('thirdpp', models.CharField(blank=True, max_length=255, null=True)), ('present_participle', models.CharField(blank=True, max_length=255, null=True)), ('past_tense', models.CharField(blank=True, max_length=255, null=True)), ('past_participle', models.CharField(blank=True, max_length=255, null=True)), ('explains', models.TextField(blank=True, null=True)), ], options={ 'db_table': 'words', 'managed': False, }, ), migrations.CreateModel( name='WordNotes', fields=[ ('id', models.BigAutoField(primary_key=True, serialize=False)), ('wordspelling', models.CharField(blank=True, db_column='wordSpelling', max_length=255, null=True)), ('uid', models.IntegerField(blank=True, db_column='UID', null=True)), ('content', models.CharField(blank=True, max_length=255, null=True)), ('difficulty_rate', models.IntegerField(blank=True, null=True)), ], options={ 'db_table': 'word_notes', 'managed': False, }, ),
-
出现sql语句为空的create操作正是由于option中managed取值所造成的
-
但是建议您从Model中修改,然后重新makemigrations
-
可以配合下方的showmigrations
命令中的列表来查看指定某个变迁所对应的sql语句映射
一般的,initial文件中记录的是初次依照app中的各个模型来创建对应的数据库表的create语句
showmigrations
, which lists a project’s migrations and their status.
-
以app 为单位进行显示
-
cxxu➜~/djangoProjects/ela(main✗)» pmg showmigrations [18:57:33] admin [X] 0001_initial [X] 0002_logentry_remove_auto_add [X] 0003_logentry_add_action_flag_choices auth [X] 0001_initial [X] 0002_alter_permission_name_max_length contenttypes [X] 0001_initial [X] 0002_remove_content_type_name scoreImprover [X] 0001_initial sessions [X] 0001_initial user [X] 0001_initial [X] 0002_remove_user_test_alter_user_examdate_and_more [X] 0003_alter_user_examdate_alter_user_examtype words [X] 0001_initial
models.py文件
manage.py makemigrations
- 该命令可以检查django app的models.py 文件中是否发生变更,并生成对应的数据库变迁操作,来同步数据库;
- 这些检查出来的变化所对应的模型操作会记录在文件中,存放在app的
migrations
目录下
- 这些检查出来的变化所对应的模型操作会记录在文件中,存放在app的
- 在执行该命令的时候,应当加上具体的app 名称,以保证makemigrations 过程能够正确执行
manage.py migrate
- 该操作可以执行
makemigrations
产生的文件,从而同步模型和数据库(表)结构
从已有的mysql数据库迁移到django项目中(django import exsited databases from mysql)
reference1
- Adding migrations to apps¶
- You have
not changed your models
since you made theirtables
. For migrations to work, you mustmake the initial migration *first*
and then make changes, as Django compares changes against migration files, not the database. - You have not manually edited your database - Django won’t be able to detect that your database doesn’t match your models, you’ll just get errors when migrations try to modify those tables.
确保initial(–fake-initial)
reference2
- How to Connect Django to an Existing Legacy Database | DevRa (rafed.github.io)
- Get an existing database
- Import the data to MySQL
- Setup Django project to work with MySQL
- Create the Django models from MySQL
- Create app and use the models
- Modifying database tables
Create the Django models from MySQL
The above below will produce django models in models.py from tables present in MySQL.
$ python manage.py inspectdb > models.py
Modifying database tables
- Legacy tables are not permitted to be modified by default.
- But they can be modified if needed.
- First set managed=True in sub Meta classes of classes you want to modify.
- And then, add the new field you want.
class Meta:
managed = True
db_table = 'customers'
Now, make the migrations file.
python manage.py makemigrations
python manage.py migrate --fake
Voila! Now the new table field should be added!