2017-04-01 1 views
1

내 Python manage.py migrate를 실행하면이 ValueError가 표시됩니다.ValueError : 밑이 10 인 int()에 대한 리터럴이 올바르지 않습니다. 'NULL'

Operations to perform: 
    Apply all migrations: admin, auth, cms, contenttypes, sessions 
Running migrations: 
    Applying cms.0002_auto_20170401_2307...Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "C:\Users\sndys\project\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line 
    utility.execute() 
    File "C:\Users\sndys\project\lib\site-packages\django\core\management\__init__.py", line 359, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Users\sndys\project\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "C:\Users\sndys\project\lib\site-packages\django\core\management\base.py", line 345, in execute 
    output = self.handle(*args, **options) 
    File "C:\Users\sndys\project\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle 
    fake_initial=fake_initial, 
    File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate 
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\migration.py", line 129, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards 
    field, 
    File "C:\Users\sndys\project\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field 
    self._remake_table(model, create_fields=[field]) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table 
    self.effective_default(field) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default 
    default = field.get_db_prep_save(default, self.connection) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\related.py", line 909, in get_db_prep_save 
    return self.target_field.get_db_prep_save(value, connection=connection) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save 
    prepared=False) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 938, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 946, in get_prep_value 
    return int(value) 
ValueError: invalid literal for int() with base 10: 'NULL' 

내 models.py 파일 : 나는 파이썬 manage.py의 makemigrations을했을 때,이 나타났다

from django.db import models 
from django.contrib.auth.models import User 
import markdown 

# Create your models here. 
class Tag(models.Model): 
    name = models.CharField(max_length=32) 
    slug = models.SlugField() 

    def __str__(self): 
     return self.name 

class Category(models.Model): 
    name = models.CharField(max_length=32) 
    slug = models.SlugField() 
    description = models.TextField() 
    image = models.ImageField() 

    def __str__(self): 
     return self.name 

class Image(models.Model): 
    name = models.CharField(max_length=32) 
    slug = models.SlugField() 
    alt_text = models.CharField(max_length=255) 
    image = models.ImageField() 

    def __str__(self): 
     return self.name 

class Gallery(models.Model): 
    name = models.CharField(max_length=32) 
    slug = models.SlugField() 
    images = models.ManyToManyField(Image) 
    description = models.TextField() 
    layout = models.CharField(
     max_length=5, 
     choices=(
      ('LINK', 'Linked'), 
      ('HORI', 'Horizontal'), 
      ('VERT', 'Vertical'))) 

    def __str__(self): 
     return self.name 

class Article(models.Model): 
    title = models.CharField(max_length=255) 
    slug = models.SlugField() 
    author = models.ForeignKey(User) 
    content = models.TextField() 
    creation_date = models.DateTimeField(auto_now_add=True) 
    edit_date = models.DateTimeField(auto_now=True) 
    featured_image = models.ImageField(null=True, blank=True) 
    publish_status = models.BooleanField(default=False) 
    category = models.ForeignKey(Category) 
    tags = models.ManyToManyField(Tag) 

    @property 
    def content_html(self): 
     return markdown.markdown(self.content) 
    def __str__(self): 
     return self.title 

    class Meta: 
     verbose_name_plural = 'Articles' 
     ordering = ['creation_date']`enter code here` 

: 당신은 nullable이 아닌 필드 '카테고리'를 추가하려고

기본값이없는 기사로; 우리는 그렇게 할 수 없습니다 (데이터베이스는 기존 행을 채우기 위해 무언가를 필요로합니다). (이 칼럼에 대한 null 값으로 기존의 모든 행에 설정됩니다)

2) 종료 지금 일회성 기본 제공)

1, 나를에 기본을 추가 할 수 있습니다 : 수정을 선택하세요

이 옵션을 선택 models.py :

나는 1을 선택하고 기본 값 "NULL"을주고, 나는 이것이 내가 만든 수있는 실수라고 생각합니다. 이 "python manage.py makemigrations"가 성공적으로 실행되었지만 "python manage.py migrate"오류가 발생했습니다. 이 다음에 기사 클래스의 카테고리 외래 키에 기본값 = 1을 지정하려고했지만 마이 그 레이션 할 때 동일한 오류가 발생합니다.

누구든지 문제가있는 곳과 해결 방법을 알려줄 수 있습니까?

+0

최신 마이그레이션 파일을 삭제하십시오. models.py 파일에 기본값을 제공하고'python manage.py makemigrations'를 다시 실행하십시오. –

+0

'null = True'를 설정하여 외래 키를 일시적으로 null로 설정 한 다음 외래 키에 적합한 값을 마이그레이션하고 삽입 할 수 있습니다. 그런 다음'null = True'를 제거하고 다시 마이그레이션하십시오. –

답변

0

DB에 수신 할 값을 제공하지 않아도됩니다. 당신은 파이썬에서 가치를 부여합니다. 그러나 null = True가 필드에 설정되지 않은 경우에만 해당 메시지를 받게됩니다. 마이그레이션을 삭제하고 해당 필드에서 null = True를 설정하십시오.

관련 문제