source

Django 이행: django.db.utils.Operation Error: (1364, "필드 '이름'에 기본값이 없습니다")

factcode 2023. 1. 19. 20:59
반응형

Django 이행: django.db.utils.Operation Error: (1364, "필드 '이름'에 기본값이 없습니다")

기본 데이터:

mysql-server: 5.7.27-0ubuntu0.16.04.1
Ubuntu: 16.04
Python: 2.7.12
Django: 1.11.16
Virtualenv

새로운 어플리케이션(갤러리)을 추가하여 다음 테이블을 만들었습니다.

class Medi(models.Model):
    gd_id = models.CharField(max_length=128)
    name = models.CharField(max_length=512, default='1')
    original_name = models.CharField(max_length=512)
    title = models.CharField(max_length=512, blank=True, null=True)
    md5sum = models.CharField(max_length=32)
    date_of_event = models.DateTimeField()
    date_of_event_end = models.DateTimeField(blank=True, null=True)
    last_updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

위의 오류가 발생한 후 정리하기 위해 데이터베이스에서 테이블, django_migrations 테이블 및 모든 마이그레이션 파일을 삭제했습니다.__init__.py.

나는 달렸다makemigrations그리고 그 이후.migrate다음과 같은 출력이 있습니다.

# python manage.py makemigrations
Migrations for 'gallery':
  gallery/migrations/0001_initial.py
    - Create model Medi

# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, bugtrack, common, contenttypes, follow, gallery, infokom, phonebook, sessions, sites
Running migrations:
  Applying gallery.0001_initial... OK
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 227, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 63, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 60, in get_for_model
    model=opts.model_name,
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 466, in get_or_create
    return self._create_object_from_params(lookup, params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 500, in _create_object_from_params
    obj = self.create(**params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 808, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 838, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 924, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 963, in _do_insert
    using=using, raw=raw)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 1079, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
    cursor.execute(sql, params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1364, "Field 'name' doesn't have a default value")

이상한 점은 오류가 발생하더라도 테이블이 생성된다는 것입니다.오브젝트를 저장할 수 있으며,또한 엔트리가 있습니다.django_migrations테이블. 그리고showmigrations에 이행이 완료되었음을 나타냅니다.단, 이 명령어를 실행하면migrate명령어를 다시 입력해도 같은 에러가 표시됩니다.

문제는 이 문제의 원인과 해결 방법입니다.mysql 설정 파일에서 STICT_TRANS_TABLES를 삭제한 후 이 오류는 경고로 바뀌지만 여전히 귀찮습니다.원인은 무엇입니까?

MySQL최신 버전에서는 디폴트값의 추가가 의무화되어 있습니다.NOT NULL컬럼을 클릭합니다.그 원인은STRICT_TRANS_TABLES모드를 수정하는 방법에는 세 가지가 있습니다.

1) null이 아닌 열에 기본값을 추가합니다.

    ALTER TABLE tablename MODIFY columname DATATYPE NOT NULL DEFAULT 'a value'

2) 기본 NULL 값 설정

    ALTER TABLE tablename MODIFY columname DATATYPE NULL

3) 인MySQL컨피규레이션파일, 삭제STRICT_TRANS_TABLES부터sql_mode서비스를 재시작합니다.

테이블은 필요없었다.테이블을 삭제한 후 마이그레이션을 실행하고 다시 마이그레이션했습니다.

언급URL : https://stackoverflow.com/questions/57840559/django-migrate-django-db-utils-operationalerror-1364-field-name-doesnt-h

반응형