ORM๐Ÿน: models.py์™€ sqlite create๋ฌธ ๋น„๊ตํ•˜๊ธฐ

์ •์€๊ฒฝยท2020๋…„ 7์›” 12์ผ
0

๐ŸŽธ Play the Djangoย 

๋ชฉ๋ก ๋ณด๊ธฐ
4/57

1. ์žฅ๊ณ ์˜ models.py

(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โšก) ยป 

2-1. sqlite์—์„œ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ” ํ™•์ธ

2-2. sqlite์—์„œ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์˜ ์Šคํ‚ค๋งˆ ํ™•์ธ

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"์€ ๋ฌด์—‡์ผ๊นŒ?

profile
#์˜์‹์˜ํ๋ฆ„ #์ˆœ๊ฐ„์ˆœ๊ฐ„ #์ƒ๊ฐ์˜์Šค๋ƒ…์ƒท

0๊ฐœ์˜ ๋Œ“๊ธ€