How to fix 'django.db.utils.OperationalError: near "None": syntax error' db.sqlite3?

How to fix 'django.db.utils.OperationalError: near "None": syntax error' db.sqlite3?

我正在尝试为我的 Django REST 框架创建一个用户模型,遗憾的是,我遇到了一些意想不到的困难:当我 运行 'python manage.py makemigrations' 一切正常时,但是,当我尝试 运行 迁移时,遇到此错误 'return Database.Cursor.execute(self, query) django.db.utils.OperationalError: near "None": syntax error'。

我已经尝试更改 models.py 中的代码,但它仍然产生相同的结果,即使我将用户模型 class 设置为只有一个 id 字段。我正在使用 django 版本 1.11.17 和 drf 版本 3.9.0

models.py:

class User(models.Model):
    id = models.AutoField(primary_key=True)

    email = models.CharField(unique=True, null=False, max_length=200)
    phone = models.IntegerField(unique=True, null=False)
    first_name = models.CharField(null=False, max_length=200)
    last_name = models.CharField(null=False, max_length=200)
    is_active = models.BooleanField(_('active'), default=False)
    last_seen = models.DateTimeField(auto_now=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)

    avatar = models.CharField(null=True)

migrations/0001_initial.py:

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
    migrations.CreateModel(
        name='User',
        fields=[
            ('id', models.AutoField(primary_key=True, serialize=False)),
            ('email', models.CharField(max_length=200, unique=True)),
            ('phone', models.IntegerField(unique=True)),
            ('first_name', models.CharField(max_length=200)),
            ('last_name', models.CharField(max_length=200)),
            ('last_seen', models.DateTimeField(auto_now=True, null=True)),
            ('updated_at', models.DateTimeField(auto_now=True, null=True)),
            ('created_at', models.DateTimeField(auto_now_add=True, null=True)),
            ('avatar', models.CharField(null=True)),
        ],
    ),
]

完整堆栈跟踪:

  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Users/my_username/myapp/myapp/lib/python2.7/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 "/Users/my_username/myapp/myapp/lib/python2.7/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 "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
    schema_editor.create_model(model)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 319, in create_model
    self.execute(sql, params or None)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 136, in execute
    cursor.execute(sql, params)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
    return Database.Cursor.execute(self, query)
  django.db.utils.OperationalError: near "None": syntax error

我希望能够成功运行迁移。

问题最终是我没有为头像字段指定 max_length,所以 django 生成的结果查询是

CREATE TABLE "user" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "email" varchar(200) NOT NULL UNIQUE, "phone" integer NOT NULL UNIQUE, "first_name" varchar(200) NOT NULL, "last_name" varchar(200) NOT NULL, "is_active" bool NOT NULL, "last_seen" datetime NULL, "updated_at" datetime NULL, "created_at" datetime NULL, "avatar" varchar(None) NULL);

和 varchar(None) 破坏了语法。你知道的越多。