(venv) (base) marie@MarieJucBookPro:~/PycharmProjects/play-with-django(play-with-ormโก) ยป cat play_with_orm_app/models.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=255)
price = models.IntegerField(default=0)
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, related_name='books')
def __str__(self):
return self.name
class Store(models.Model):
name = models.CharField(max_length=255)
books = models.ManyToManyField(Book, related_name='stores')
def __str__(self):
return self.name
(venv) (base) marie@MarieJucBookPro:~/PycharmProjects/play-with-django(play-with-ormโก) ยป
1) Publisher ํด๋์ค
CREATE TABLE "play_with_orm_app_publisher"
("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(255) NOT NULL)
2) Book ํด๋์ค
CREATE TABLE "play_with_orm_app_book"
("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(255) NOT NULL,
"price" integer NOT NULL,
"publisher_id" integer NOT NULL REFERENCES "play_with_orm_app_publisher" ("id") DEFERRABLE INITIALLY DEFERRED)
3) Store ํด๋์ค (Many-to-Many ํ๋๋ก books๋ผ๋ ์์ฑ์ ์ฐ๊ฒฐ)
CREATE TABLE "play_with_orm_app_store"
("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(255) NOT NULL)
4) ๐ store_books ํ
์ด๋ธ (many-to-many field๋ก ์๋ ์์ฑ๋จ)
Storeํด๋์ค์์ books๋ผ๋ ํ
์ด๋ธ๊ณผ ์ฐ๊ฒฐํ๋ ๋งตํํ
์ด๋ธ์ธ! "play_with_orm_app_store_books"
- ์ฌ๊ธฐ์ ๋งตํํ ์ด๋ธ ์ด๋ฆ์ ๋ค์ด๋ฐ๊ท์น:
"์ฅ๊ณ ์ฑ์ด๋ฆ_'๋งค๋ํฌ๋งค๋ํ๋์ค์ ์ ํ ํด๋์ค์ด๋ฆ'_'๋งค๋ํฌ๋งค๋ํ๋์ค์ ์ด ๋ค์ด๊ฐ ํด๋์ค์์ฑ์ด๋ฆ'
CREATE TABLE "play_with_orm_app_store_books"
("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"store_id" integer NOT NULL REFERENCES "play_with_orm_app_store" ("id") DEFERRABLE INITIALLY DEFERRED,
"book_id" integer NOT NULL REFERENCES "play_with_orm_app_book" ("id") DEFERRABLE INITIALLY DEFERRED)
"DEFERRABLE INITIALLY DEFERRED"์ ๋ฌด์์ผ๊น?