gpt4 book ai didi

python - 如何修复 'django.db.utils.OperationalError: near "无“: syntax error' db. sqlite3?

转载 作者:行者123 更新时间:2023-12-01 01:20:05 25 4
gpt4 key购买 nike

我正在尝试为我的 Django REST 框架创建一个用户模型,遗憾的是,我遇到了一些意想不到的困难:当我运行“python manage.py makemigrations”时,一切都按其应有的方式运行,但是,当我尝试运行迁移,我收到此错误“return Database.Cursor.execute(self, query) django.db.utils.OperationalError:near“None”:语法错误”。

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

模型.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)

迁移/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) 破坏了语法。你知道的越多。

关于python - 如何修复 'django.db.utils.OperationalError: near "无“: syntax error' db. sqlite3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53916790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com