Many to many relationship
M:N κ΄κ³
β Many-to-many relationships
β ν ν
μ΄λΈμ 0κ° μ΄μμ λ μ½λκ° λ€λ₯Έ ν
μ΄λΈμ 0κ° μ΄μμ λ μ½λμ κ΄λ ¨λ κ²½μ°
β μμͺ½ λͺ¨λμμ N:1 κ΄κ³λ₯Ό κ°μ§λ€.
κ°μ
β λ³μμ λ΄μνλ νμμ μμ¬μ μμ½ μμ€ν
ꡬμΆ
β νμ <-> μμ¬ κ΄κ³ λ°μ΄ν° λͺ¨λΈλ§
μ©μ΄ μ 리
β Target model
- κ΄κ³ νλλ₯Ό κ°μ§μ§ μμ λͺ¨λΈ
β Source model
- κ΄κ³ νλλ₯Ό κ°μ§ λͺ¨λΈ
N:1 λͺ¨λΈμ νκ³
![image](https://user-images.githubusercontent.com/109324637/195232355-c0012417-54d9-44fd-9d6a-95082b762a67.png)
β λμΌν νμκ° λ€λ₯Έ μμ¬μκ² μμ½νκΈ° μν΄μλ κ°μ²΄λ₯Ό νλ λ λ§λ€μ΄μ μμ½μ μ§νν΄μΌ νλ€.
![image](https://user-images.githubusercontent.com/109324637/195232416-68b9415c-0ccf-4439-b89f-c17088a86e0b.png)
β μΈλ ν€ μ»¬λΌμ '1, 2' ννλ‘ μ°Έμ‘°νκΈ° λΆκ°λ₯
μ€κ° λͺ¨λΈ μμ±μ ν΅ν ν΄κ²°
![image](https://user-images.githubusercontent.com/109324637/195232961-fad40963-d8ee-438b-8fe0-a6bd42ffaa3f.png)
β νμμ μμ¬ λλ€μ λν΄μ N:1 κ΄κ³λ₯Ό κ°μ§λ λ³λμ μμ½ λͺ¨λΈμ μλ‘ μμ±
![image](https://user-images.githubusercontent.com/109324637/195233197-bfb7c230-a661-42a9-bcad-1723c11b1fd0.png)
β μμ¬μ νμ μμ± ν μμ½ λ§λ€κΈ° (μ¬μ§μ doctorλ₯Ό λ λ² μμ±ν΄μ 2λ² μμ¬λ‘ λμ΄)
![image](https://user-images.githubusercontent.com/109324637/195233571-790c08a9-f204-461c-a907-f044e5ed8e68.png)
β μμ°Έμ‘°λ₯Ό ν΅ν΄μ μμ½ μ 보 μ‘°ν κ°λ₯
Django ManyToManyField
![image](https://user-images.githubusercontent.com/109324637/195233888-d922b5d0-d1f8-44e5-9eec-40611439d0ca.png)
β νμ λͺ¨λΈμ DJango ManyToManyField μμ± (볡μνμΌλ‘ μμ±νκΈ°!)
![image](https://user-images.githubusercontent.com/109324637/195234574-c4210f74-3814-4e45-b793-4e6424d9a298.png)
β μ’ μμ μ΄μ§ μμ λ³λμ μ€ν€λ§(μ€κ° ν μ΄λΈ)μ΄ μμ±λλ€.
![image](https://user-images.githubusercontent.com/109324637/195235073-e8981805-0640-4032-bd03-6437fb103297.png)
β μμ½ μμ± λ° μ‘°ν(νμ -> μμ¬)
![image](https://user-images.githubusercontent.com/109324637/195235264-a5d8db9b-b008-42a0-b807-883f1f52c1dd.png)
β μμ¬ -> νμ
![image](https://user-images.githubusercontent.com/109324637/195235894-76eb0b45-9687-4858-bdbc-762ab94862c0.png)
β μμ½ μ·¨μ (μμ¬ -> νμ)
![image](https://user-images.githubusercontent.com/109324637/195236059-84efdc25-3892-4b89-b81a-178f02bdedee.png)
β μμ½ μ·¨μ (νμ -> μμ¬)
related_name
argument
β target modelμ΄ source modelμ μ°Έμ‘°ν λ μ¬μ©ν μ΄λ¦
![image](https://user-images.githubusercontent.com/109324637/195236315-e5b740df-fc9b-4f31-942f-db74c02d730d.png)
![image](https://user-images.githubusercontent.com/109324637/195236513-b6646cd1-e91a-4d1a-8dab-56837d91bbdf.png)
β μμ°Έμ‘°κ° μλλΌ related_nameμΌλ‘ λ°λ‘ μ κ·Όν μ μλ€
through
argument
β κ·Έλ λ€λ©΄ μ€κ° λͺ¨λΈμ μ§μ μμ±νλ κ²½μ°λ μμκΉ?
- μ€κ° ν
μ΄λΈμ μλμΌλ‘ μ§μ ν κ²½μ°
through
μ΅μ μ μ¬μ©ν μ μλ€.
β κ°μ₯ μΌλ°μ μΈ μ©λλ μ€κ°ν μ΄λΈμ μΆκ° λ°μ΄ν°λ₯Ό μ¬μ©ν΄ M:Nκ΄κ³μ μ°κ²°νλ €λ κ²½μ°
![image](https://user-images.githubusercontent.com/109324637/195237266-8420a688-fb62-4fde-8e3b-d4372a44798c.png)
β through
μ€μ λ° Reservation Class
μμ
![image](https://user-images.githubusercontent.com/109324637/195237833-baf83506-ffca-44d8-bd0a-9e227867a3dc.png)
β κΈ°μ‘΄ μ€κ° λͺ¨λΈμμμ μμ½ μμ± λ°©μμ μ΄μ©ν μλ μλ€.
![image](https://user-images.githubusercontent.com/109324637/195238113-5b64b42a-b1d1-4a0d-a72e-1f3f9c011a2a.png)
β κΈ°μ‘΄ manytomany fieldμμ μμ¬λ νμκ° μμ½μ μμ±νλ―μ΄ μμ±ν μ μλ€.
β through_dfaults
κ°μ λμ
λ리 νμ
μΌλ‘ μ
λ ₯ν΄μ€λ€.
symmetrical
β λμΉμ μλ―Έ
β ManyToManyFieldκ° λμΌν λͺ¨λΈ(on self)μ κ°λ¦¬ν€λ μ μμμ μ¬μ©
![image](https://user-images.githubusercontent.com/109324637/195239286-986b379e-56a7-4b28-be2d-6b82e413534a.png)
β TrueμΌ κ²½μ°
- _set 맀λμ λ₯Ό μΆκ°νμ§ μμ
- source λͺ¨λΈμ μΈμ€ν΄μ€κ° targetλͺ¨λΈμ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°νλ©΄ μλμΌλ‘ targetλͺ¨λΈ μΈμ€ν΄μ€λ source λͺ¨λΈ μΈμ€ν΄μ€λ₯Ό μλμΌλ‘ μ°Έμ‘°νλλ‘ νλ€.(λμΉ)
- μ¦, λ΄κ° λΉμ μ μΉκ΅¬λΌλ©΄ λΉμ λ λ΄ μΉκ΅¬κ° λλ κ΄κ³
β λμΉμ μνμ§ μλ κ²½μ° Falseλ‘ μ€μ
μ 리
β M:N κ΄κ³λ‘ λ§Ίμ΄μ§ λ ν
μ΄λΈμλ λ³νκ° μμ
β Djangoμ ManyToManyField
μ μ€κ° ν
μ΄λΈμ μλμΌλ‘ μμ±νλ€.
β Djangoμ ManyToManyField
λ M:N κ΄κ³λ₯Ό λ§Ίλ λ λͺ¨λΈ μ΄λμ μμΉν΄λ μκ΄ μμΌλ, νλ μμ± μμΉμ λ°λΌ μ°Έμ‘°μ μμ°Έμ‘° λ°©ν₯μ μ£Όμν΄μΌ νλ€.
β N:1μ μμ ν μ’
μ κ΄κ³ μμ§λ§, M:Nμ μμ¬μκ² μ§μ°°λ°λ νμ, νμλ₯Ό μ§μ°°νλ μμ¬μ λ κ°μ§ ννλ‘ λͺ¨λ νν κ°λ₯
'β Personal_Study > Database' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
νλ‘μ νΈ μ DB μ€κ³ λ°©λ² (0) | 2023.03.08 |
---|---|
λ°μ κ·ν (0) | 2023.03.07 |
N:1 Relationship (1) | 2022.10.08 |
Relational Database (0) | 2022.10.08 |
DML (Data Manipulation Language) (0) | 2022.10.08 |
λκΈ