日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

Django(part33)--数据库的迁移

發(fā)布時(shí)間:2023/12/19 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django(part33)--数据库的迁移 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(xué)習(xí)筆記,僅供參考


數(shù)據(jù)庫(kù)的遷移


我在學(xué)習(xí)一對(duì)多映射時(shí),由于操作不慎,導(dǎo)致報(bào)錯(cuò)頻頻,現(xiàn)在,我就來(lái)解決這個(gè)問題,順便學(xué)習(xí)一下遷移操作。

現(xiàn)在,我在第7次遷移時(shí)出錯(cuò)了,它的錯(cuò)誤是這樣的:

pymysql.err.InternalError: (1054, "Unknown column 'pub' in 'china_publisher'")

報(bào)錯(cuò)信息顯示,pub字段不在china_publisher表中。

我修改之后,還有一個(gè)報(bào)錯(cuò),它是這樣的:

django.db.utils.InternalError: (1366, "Incorrect integer value: '清華大學(xué)出版社' for column 'pub_id' at row 1")

錯(cuò)誤太多,我也不知道該怎么辦,所以我選擇回到前幾次遷移文件,重新開始。


看一下項(xiàng)目目錄,方便后續(xù)理解:


目前,我在第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,發(fā)現(xiàn)Django還是可能會(huì)報(bào)錯(cuò),就再退到0002,發(fā)現(xiàn)Django依然可能會(huì)報(bào)錯(cuò),最后退到0001,也就是說(shuō)我敲入了3次退回代碼。上面顯示的輸出,是我從0002退回到0001的輸出。


我們查看一下數(shù)據(jù)庫(kù):

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)

此時(shí)的數(shù)據(jù)庫(kù)應(yīng)該沒有china_publisher數(shù)據(jù)表,不知道哪里出錯(cuò)了,這種情況和Django在0001遷移文件里記錄的情況完全不同,所以我果斷將其刪除!

mysql> drop table china_publisher; Query OK, 0 rows affected (0.03 sec)

一般情況下,還是不要隨便刪表,要不然很容易出現(xiàn)混亂。


現(xiàn)在,我們把0001以上的記錄文件全部刪除:

并再次進(jìn)行遷移操作

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

遷移成功!


查看一下數(shù)據(jù)表:

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開發(fā)實(shí)戰(zhàn) | 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)

這時(shí),我們?cè)贐ook模型類中再加入一個(gè)字段pub,并與Publisher模型類進(jìn)行一對(duì)多關(guān)聯(lián):

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("聯(lián)系電話", 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("出廠價(jià)", max_digits = 6, decimal_places = 2,default = 0)price = models.DecimalField("售價(jià)", 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)

再次執(zhí)行遷移操作:

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數(shù)據(jù)表:

mysql> select * from bookstore_book; +----+-------------------+------------+-------+--------+ | id | title | exfacPrice | price | pub_id | +----+-------------------+------------+-------+--------+ | 1 | Djangoweb開發(fā)實(shí)戰(zhàn) | 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)

總結(jié)

以上是生活随笔為你收集整理的Django(part33)--数据库的迁移的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。