django 模型生成sql(多對多)

2022-08-25 16:12:32 字數 3960 閱讀 6915

模型如下:

class

publisher(models.model):

name = models.charfield(max_length=30)

address = models.charfield(max_length=50)

city = models.charfield(max_length=60)

state_province = models.charfield(max_length=30)

country = models.charfield(max_length=50)

website =models.urlfield()

class

author(models.model):

first_name = models.charfield(max_length=30)

last_name = models.charfield(max_length=40)

email =models.emailfield()

class

book(models.model):

title = models.charfield(max_length=100)

authors =models.manytomanyfield(author)

publisher =models.foreignkey(publisher)

publication_date = models.datefield()

檢視生成sql用》python manage.py sqlmigrate,

生成sql:

begin;--

下面來解釋自動生成的sql語句,生成的sql順序比較亂,已經整理了一下

create

table

`posts_author` (

`id`

integer auto_increment not

null

primary

key,

`first_name`

varchar(30) not

null

, `last_name`

varchar(40) not

null

, `email`

varchar(254) not

null

);--

----

create

table

`posts_book` (

`id`

integer auto_increment not

null

primary

key,

`title`

varchar(100) not

null

, `publication_date` date

notnull

);--

create

table

`posts_book_authors` (

`id`

integer auto_increment not

null

primary

key,

`book_id`

integer

notnull

, `author_id`

integer

notnull

);--

--這個表是沒有在models中定義的,只是因為authors = models.manytomanyfield(author)

--django就建立了乙個表(多對多連線表)來處理書籍和作者之間的對映關係

--這也是資料庫設計的原則,這樣能較少冗餘。不然的話,比如說一本書有多個作者,這個表就會重複多條資料(除了作者不同)

--create

table

`posts_publisher` (

`id`

integer auto_increment not

null

primary

key,

`name`

varchar(30) not

null

, `address`

varchar(50) not

null

, `city`

varchar(60) not

null

, `state_province`

varchar(30) not

null

, `country`

varchar(50) not

null

, `website`

varchar(200) not

null

);--

--上面分別建立的表,因為模型沒有指明關鍵字,所以自動建立id為關鍵字

--alter

table `posts_book` add

column `publisher_id` integer

notnull

;alter

table `posts_book` alter

column `publisher_id` drop

default

; alter

table `posts_book` add

constraint `posts_book_publisher_id_4475e105_fk_posts_publisher_id` foreign

key (`publisher_id`) references

`posts_publisher` (`id`);

----

publisher = models.foreignkey(publisher)

--這句話的意思是『參考類publisher(也就是表posts_publisher)』

--建立外來鍵,表publisher主鍵是id,所以會自動加上乙個欄位publisher_id

--最後就是在表posts_book 中增加外來鍵 `posts_publisher` (`id`)

--alter

table `posts_book_authors` add

constraint `posts_book_authors_book_id_1487a223_fk_posts_book_id` foreign

key (`book_id`) references

`posts_book` (`id`);

alter

table `posts_book_authors` add

constraint `posts_book_authors_author_id_0a9b3d34_fk_posts_author_id` foreign

key (`author_id`) references

`posts_author` (`id`);

alter

table `posts_book_authors` add

constraint `posts_book_authors_book_id_7d4dbc86_uniq` unique

(`book_id`, `author_id`);

----

這裡又建立了兩個外來鍵,都是中間表『外來鍵』其他兩個表,分別是

--表posts_book_authors(book_id) 外來鍵 posts_book(id)

--表posts_book_authors(author_id) 外來鍵 posts_author(id)

--最後做了唯一性約束

--create

index `posts_book_2604cbea` on

`posts_book` (`publisher_id`);

----

可以看出,都是表建立完成後,才進行外來鍵設定操作

--commit;

Django入門 多對一模型

對於乙個初學者,當看到django中models使用多對一時,一臉的mb!q 為什麼要有一對 一 多對 一 多對多的模型?a 乙個系統中經常會有文章 資料等的對應關係,比如 乙個賬戶只有乙個使用者名稱 乙個賬戶對應這個使用者名稱,這個使用者名稱就是指這個賬戶 乙個作者發布了多個blog 知道這個作者...

Django多對多操作

模型 from django.db import models 老師 class teacher models.model name models.charfield max length 32 學生 class student models.model name models.charfield ...

django模型多對一 多對多 一對一三種關係解讀

作用 設計的好,會清晰,且易於理解,後續開發也事半功倍,易於維護。基本原則 1.一對一的表,兩表的屬性實際上完全可以合併成乙個表,共用乙個主鍵即可 2.一對多的表,可以設中間關聯表,也可以將關聯表併入 多 這頭 若設獨立關聯表,則可引入 多 這頭的主鍵作為其主鍵,也可另立主鍵並將 一 和 多 兩表的...