๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Programming/error-record

[Error] Django makemigrations : DB new column ์ถ”๊ฐ€ ํ›„ ๋ฐ˜์˜

Django์—์„œ ์ตœ์ดˆ์— makemigrations์„ ํ•œ ํ›„, ์ƒˆ ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์‹œ makemigrations์„ ์‹œ๋„ํ•˜๋ฉด์„œ ์ƒ๊ธด ์—๋Ÿฌ์ด๋‹ค.

 

โ“Error Message

>> python manage.py makemigrations

You are trying to add a non-nullable field 'name_eng' to menu without a default; we can't do that (the database needs something to populate existing rows).

Please select a fix:

 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)

 2) Quit, and let me add a default in models.py

Select an option:

 

์—๋Ÿฌ ๋ฉ”์„ธ์ง€!


๐Ÿ” ์—๋Ÿฌ ํ•ด์„ํ•˜๊ธฐ

# models.py
# ์ตœ์ดˆ migrateํ•œ migrations ์ฝ”๋“œ
class Menu(models.Model):
    name = models.CharField(max_length=80)
    description = models.CharField(max_length=120)
    price = models.IntegerField()
    img_path = models.CharField(max_length=255)
    def __str__(self):
        return self.name
        
       
# migrations ์ˆ˜์ •์„ ์œ„ํ•œ ์ฝ”๋“œ
class Menu(models.Model):
    name = models.CharField(max_length=80)
    name_eng = models.CharField(max_length=80)
    description = models.CharField(max_length=120)
    price = models.IntegerField()
    img_path = models.CharField(max_length=255)
    def __str__(self):
        return self.name

์œ„์˜ ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์ฐธ๊ณ ํ•˜๋ฉด, name_eng์ปฌ๋Ÿผ์ด ์ถ”๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์—๋Ÿฌ ๋ฉ”์„ธ์ง€์˜ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ํ•ด์„์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. (by papago ๐Ÿฆœ)

null์ด ์•„๋‹Œ ํ•„๋“œ 'name_eng'์„ ๊ธฐ๋ณธ๊ฐ’ ์—†์ด ๋ฉ”๋‰ด์— ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ธฐ์กด ํ–‰์„ ์ฑ„์›Œ์•ผ ํ•จ).
์ˆ˜์ • ์‚ฌํ•ญ์„ ์„ ํƒํ•˜์‹ญ์‹œ์˜ค.
1) ์ง€๊ธˆ ์ผํšŒ์„ฑ ๊ธฐ๋ณธ๊ฐ’ ์ œ๊ณต(์ด ์—ด์— ๋Œ€ํ•ด null ๊ฐ’์œผ๋กœ ๊ธฐ์กด ๋ชจ๋“  ํ–‰์— ์„ค์ •๋จ)
2) ์ข…๋ฃŒํ•˜๊ณ  ๋ชจ๋ธ์— ๊ธฐ๋ณธ๊ฐ’์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
์˜ต์…˜ ์„ ํƒ :

์ฆ‰, ์ƒˆ ์ปฌ๋Ÿผ์ด ์ถ”๊ฐ€๋˜๊ธฐ์ „ ๊ธฐ์กด์˜ row, ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด ์„ ํƒ์„ ํ•ด๋‹ฌ๋ผ๋Š” ๋ง์ด๋‹ค.

1๋ฒˆ์€ ์ฑ„์›Œ๋„ฃ์„ ๊ฐ’์„ ์ง์ ‘ ์ž…๋ ฅํ•ด์„œ ๋„ฃ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ๋กœ, ํ•ด๋‹น ์ปฌ๋Ÿผ ์ž๋ฃŒํ˜•์— ๋งž๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ๋Š” CharField์— ๋งž๊ฒŒ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์œผ๋กœ ์ฑ„์›Œ์ฃผ์–ด์•ผํ•œ๋‹ค.

2๋ฒˆ์€ ์ค‘๋‹จํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ์ด๋Ÿฐ ๋Š๋‚Œ์ด๋‹ค.

์–ด๋–ป๊ฒŒ ํ•ด์š”!


โ—๏ธsolution code

1. migrate ํ•˜๊ธฐ ์ „์ด๋ผ๋ฉด

์ƒ์„ฑ๋œ migration ํŒŒ์ผ์„ ์‚ญ์ œํ•˜์‹œ๊ณ  ๋‹ค์‹œ ์‹คํ–‰ ๊ฐ€๋Šฅ

์ด๋ฏธ ์ƒ์„ฑ๋˜์–ด์žˆ๋‹ค๋ฉด ๋ถˆ๊ฐ€๋Šฅ! 2๋ฒˆ ๋ฐฉ๋ฒ•์œผ๋กœ โคต๏ธ

* ์‚ญ์ œํ•  ๋•Œ๋Š” migarionsํด๋”์˜ ํŒŒ์ผ๊ณผ, db.sqlite3 ํŒŒ์ผ์„ ๊ฐ™์ด ์‚ญ์ œํ•ด์•ผํ•จ

2. migrate๋ฅผ ํ†ตํ•ด ์ด๋ฏธ DB์— ์ ์šฉํ–ˆ๋‹ค๋ฉด

์•„๋ž˜์˜ ์ฝ”๋“œ ์ค‘ ํ•˜๋‚˜๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค. 

case 1๋ฒˆ์€ ๋นˆ ๋ฌธ์ž์—ด, case 2๋ฒˆ์€ "์ฑ„์›Œ๋„ฃ์„ ๊ฐ’"์œผ๋กœ ์ฑ„์›Œ์ง€๊ฒŒ ๋œ๋‹ค.

# models.py
# case 1
name_eng = models.CharField(max_length=80, blank=True)
# case 2
name_eng = models.CharField(max_length=80, default='์ฑ„์›Œ๋„ฃ์„ ๊ฐ’')

 


 

๐Ÿ™ reference

์ฝ”๋“œ์ž‡ ๊ฐ•์˜๋ฅผ ๋“ฃ๋˜ ์ค‘ ๋ฐœ๊ฒฌํ•œ QnA๋ฅผ ํ†ตํ•ด ์›ํ™œํ•˜๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹ค!

์ฝ”๋“œ์ž‡ ๋“ฑ๊ธ‰ํ‘œ์—์„œ ์–ด๋Š๋ง SUN์„ ๋‹ฌ์„ฑํ• ๋งŒํผ ๋งŽ์ด ๋“ค์—ˆ๋Š”๋ฐ ๋งค๋ฒˆ QnA์—์„œ ํฐ ๋„์›€ ์ฃผ์‹œ๋Š” ๋ž‘์ด๋‹˜ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹คโ˜บ๏ธ