티스토리 뷰

728x90
반응형

우선 저는 저 오류가 났을 때 아래와 같은 명령어를 실행했을 때 났습니다.

python manage.py makemigrations
python manage.py migrate

저는 makemigrations 까지는 잘 되었는데, migrate 하니깐 오류가 막 나면서 아래와 같은 결과가 나왔습니다.

C:\Users\DomDomi\Desktop\Crawler\new-crawler-server>python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: actor, community, crawler, user
  Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table user_user
    Creating table group_group
    Creating table feed_discussion
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 414, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "[]": syntax error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\DomDomi\Desktop\Crawler\new-crawler-server\manage.py", line 22, in <module>
    main()
  File "C:\Users\DomDomi\Desktop\Crawler\new-crawler-server\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Python39\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "C:\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python39\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python39\lib\site-packages\django\core\management\base.py", line 417, in execute
    output = self.handle(*args, **options)
  File "C:\Python39\lib\site-packages\django\core\management\base.py", line 90, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Python39\lib\site-packages\django\core\management\commands\migrate.py", line 223, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "C:\Python39\lib\site-packages\django\core\management\commands\migrate.py", line 358, in sync_apps
    editor.create_model(model)
  File "C:\Python39\lib\site-packages\django\db\backends\base\schema.py", line 355, in create_model
    self.execute(sql, params or None)
  File "C:\Python39\lib\site-packages\django\db\backends\base\schema.py", line 151, in execute
    cursor.execute(sql, params)
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Python39\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "C:\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 414, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "[]": syntax error

 

오류의 제일 하단에는 django.db.utils.OperationalError: near "[]": syntax error 라고 하는데, 해당 오류가 나는 이유를 추적해보면 결국 일반적인 상황에서는 나지 않는 경우라는 것을 알 수 있었습니다.

 

만약 해당 오류가 나는 분이라면, 혹시 아래 사항에 해당되시는 지 확인해볼 필요가 있습니다.

  • Django 에서 postgre DB 에서 sqlite3 DB로 변경하시려는 분

이제 본격적으로 오류의 원인을 확인해보도록 하겠습니다.

Django 의 Models 나 Migrations 파일에서 아래와 같은 코드를 확인할 수 있습니다.

from django.db import models
from django.contrib.postgres.fields import ArrayField

# Create your models here.
class Discussion(models.Model):
    create_date = models.DateField("생성일", auto_now_add=False, blank=True, null=True)
    title = models.CharField("제목", max_length=300, default="", blank=True)
    content = models.TextField("본문", default="", blank=True)
    favorites = ArrayField(models.CharField(max_length=20), blank=True, default=list)

    def __str__(self):
        return f"{self.title}"

위 코드는 제가 Django에서 postgre DB를 사용하던 때 쓰던 코드입니다. 근데 제가 postgre DB에서 sqlite3 DB로 변경하던 차에 오류가 났는데 그 이유는 사실 sqlite3 에서는 ArrayField 타입이 없어서 생긴 문제입니다.

 

만약 저와 똑같은 상황에서 postgre db 코드를 더이상 사용하지 않고, sqlite3 db 로 변경하시고자 하신다면 아래와 같이 ArrayField 를 제거 해주시면 문제는 해결됩니다.

from django.db import models

# Create your models here.
class Discussion(models.Model):
    create_date = models.DateField("생성일", auto_now_add=False, blank=True, null=True)
    title = models.CharField("제목", max_length=300, default="", blank=True)
    content = models.TextField("본문", default="", blank=True)
    favorites = models.CharField("관심사", max_length=300, default="", blank=True)

    def __str__(self):
        return f"{self.title}"

 

생각보다 별 것 아닌 문제였죠? 이런 문제는 일반적으로 많이 나타나지 않는 문제고, 만약 저처럼 중간에 db 를 변경하는 분이 아니신데 이런 오류가 났다면, 한번쯤 models 나 migration 관련 파일에서 django 문서에 맞지 않는 사용법을 사용하셨거나 오타가 있진 않으신지 확인해보셔야할 것 같습니다.

 

- 오류해결 끝 -

728x90
반응형
댓글