我正在尝试为Django REST框架创建用户模型,可悲的是,我遇到了一些意想不到的困难:当我运行python manage.py makemigrations时,所有功能均应正常运行,但是,当我尝试运行运行迁移,我收到以下错误消息:“返回Database.Cursor.execute(自我,查询)django.db.utils.OperationalError:靠近“无”:语法错误”。
我尝试更改models.py中的代码,但是即使我将用户模型类设置为仅具有一个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
我希望能够成功运行迁移。
答案 0 :(得分:2)
问题最终是我没有为头像字段指定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)破坏了语法。了解更多。