Django(part33)--数据库的迁移
生活随笔
收集整理的這篇文章主要介紹了
Django(part33)--数据库的迁移
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習筆記,僅供參考
數據庫的遷移
我在學習一對多映射時,由于操作不慎,導致報錯頻頻,現在,我就來解決這個問題,順便學習一下遷移操作。
現在,我在第7次遷移時出錯了,它的錯誤是這樣的:
pymysql.err.InternalError: (1054, "Unknown column 'pub' in 'china_publisher'")報錯信息顯示,pub字段不在china_publisher表中。
我修改之后,還有一個報錯,它是這樣的:
django.db.utils.InternalError: (1366, "Incorrect integer value: '清華大學出版社' for column 'pub_id' at row 1")錯誤太多,我也不知道該怎么辦,所以我選擇回到前幾次遷移文件,重新開始。
看一下項目目錄,方便后續理解:
目前,我在第7次遷移的位置,我想回到第1次遷移,那該咋整呢?
我們可以在cmd中敲入如下代碼:
python manage.py migrate bookstore 0001輸出:
Operations to perform:Target specific migration: 0001_initial, from bookstore Running migrations:Rendering model states... DONEUnapplying bookstore.0002_author... OK這里,我從0007退到0005,發現Django還是可能會報錯,就再退到0002,發現Django依然可能會報錯,最后退到0001,也就是說我敲入了3次退回代碼。上面顯示的輸出,是我從0002退回到0001的輸出。
我們查看一下數據庫:
mysql> show tables; +----------------------------+ | Tables_in_mywebdb | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | bookstore_book | | china_publisher | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 12 rows in set (0.00 sec)此時的數據庫應該沒有china_publisher數據表,不知道哪里出錯了,這種情況和Django在0001遷移文件里記錄的情況完全不同,所以我果斷將其刪除!
mysql> drop table china_publisher; Query OK, 0 rows affected (0.03 sec)一般情況下,還是不要隨便刪表,要不然很容易出現混亂。
現在,我們把0001以上的記錄文件全部刪除:
并再次進行遷移操作
F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_db>python manage.py ma kemigrations Migrations for 'bookstore':bookstore\migrations\0002_auto_20200622_0213.py- Create model Author- Create model Publisher- Remove field pub from book- Add field exfacPrice to book- Add field price to book- Create model PartnerF:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_db>python manage.py mi grate Operations to perform:Apply all migrations: admin, auth, bookstore, contenttypes, sessions Running migrations:Applying bookstore.0002_auto_20200622_0213... OK遷移成功!
查看一下數據表:
mysql> show tables; +----------------------------+ | Tables_in_mywebdb | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | bookstore_author | | bookstore_book | | bookstore_partner | | china_publisher | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 14 rows in set (0.00 sec)mysql> select * from china_publisher; Empty set (0.00 sec)mysql> select * from bookstore_book; +----+-------------------+------------+-------+ | id | title | exfacPrice | price | +----+-------------------+------------+-------+ | 1 | Djangoweb開發實戰 | 0.00 | 0.00 | | 2 | python | 0.00 | 0.00 | | 3 | R | 0.00 | 0.00 | | 5 | 算法 | 0.00 | 0.00 | | 6 | 集體智慧編程 | 0.00 | 0.00 | +----+-------------------+------------+-------+ 5 rows in set (0.00 sec)這時,我們在Book模型類中再加入一個字段pub,并與Publisher模型類進行一對多關聯:
from django.db import models# Create your models here.class Publisher(models.Model):name = models.CharField("出版社名", max_length = 50,null = True)booknumber = models.PositiveIntegerField("初版書籍總量", default = 0)tele = models.CharField("聯系電話", max_length = 11, null = False)class Meta:db_table = "china_publisher"verbose_name = "ChinaPublisher"verbose_name_plural = "ChinaPublishers"def __str__(self):string = "出版社:%s" % (self.name)return stringclass Book(models.Model):title = models.CharField("書名", max_length = 30)exfacPrice = models.DecimalField("出廠價", max_digits = 6, decimal_places = 2,default = 0)price = models.DecimalField("售價", max_digits = 6, decimal_places = 2,default = 0)pub = models.ForeignKey(Publisher, on_delete = models.CASCADE , null=True)def __str__(self):string = "書名:%s" % (self.title) return stringclass Author(models.Model):name = models.CharField("姓名", max_length = 30, null = False, unique = True, db_index = True)age = models.IntegerField("年齡", null = False,default = 1)email = models.EmailField("郵箱", null = True)def __str__(self):string = "姓名:{}, 年齡:{}".format(self.name, self.age) return stringclass Partner(models.Model):'''作家伴侶模型類'''name = models.CharField("姓名", max_length=50)age = models.IntegerField("年齡", null = False,default = 1)author = models.OneToOneField(Author, on_delete = models.CASCADE)再次執行遷移操作:
F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_db>python manage.py makemigrations Migrations for 'bookstore':bookstore\migrations\0003_book_pub.py- Add field pub to bookF:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_db>python manage.py migrate Operations to perform:Apply all migrations: admin, auth, bookstore, contenttypes, sessions Running migrations:Applying bookstore.0003_book_pub... OK遷移成功!
再次查看bookstore_book數據表:
mysql> select * from bookstore_book; +----+-------------------+------------+-------+--------+ | id | title | exfacPrice | price | pub_id | +----+-------------------+------------+-------+--------+ | 1 | Djangoweb開發實戰 | 0.00 | 0.00 | NULL | | 2 | python | 0.00 | 0.00 | NULL | | 3 | R | 0.00 | 0.00 | NULL | | 5 | 算法 | 0.00 | 0.00 | NULL | | 6 | 集體智慧編程 | 0.00 | 0.00 | NULL | +----+-------------------+------------+-------+--------+ 5 rows in set (0.00 sec)總結
以上是生活随笔為你收集整理的Django(part33)--数据库的迁移的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原神古老的石碑怎么进去
- 下一篇: Django(part34)--一对多映