Django修改模型字段后migrate报错ValueError:无法引用列表参数
解决SQLite下ArrayField转CharField的迁移错误
问题场景
将Hotel模型的people字段从PostgreSQL专属的ArrayField改为CharField后,执行migrate命令时触发错误。
当前模型代码
class Hotel(models.Model): name = models.CharField(max_length=255) address = models.CharField(max_length=255) check_in_date = models.DateField() check_out_date = models.DateField() notes = models.TextField(blank=True) id = models.AutoField(primary_key=True) people = models.CharField(max_length=255) def __str__(self): return self.name + ", " + str(self.id)
相关迁移文件
0007(添加ArrayField):
class Migration(migrations.Migration): dependencies = [ ('project_name', '0006_confirmationentry_alter_hotel_notes'), ] operations = [ migrations.AddField( model_name='hotel', name='people', field=django.contrib.postgres.fields.ArrayField(base_field=models.IntegerField(), default=list, size=None), ), ]
0008(改为CharField):
class Migration(migrations.Migration): dependencies = [ ('project_name', '0007_hotel_people'), ] operations = [ migrations.AlterField( model_name='hotel', name='people', field=models.CharField(max_length=255), ), ]
迁移错误信息
Applying project_name.0007_hotel_people...Traceback (most recent call last): File "/Users/me/Desktop/project_name/manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv self.execute(*args, **cmd_options) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 98, in wrapped res = handle_func(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 290, in handle post_migrate_state = executor.migrate( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/executor.py", line 131, in migrate state = self._migrate_all_forwards( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/executor.py", line 163, in _migrate_all_forwards state = self.apply_migration( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/executor.py", line 248, in apply_migration state = migration.apply(state, schema_editor) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/migration.py", line 131, in apply operation.database_forwards( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 108, in database_forwards schema_editor.add_field( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/schema.py", line 381, in add_field self._remake_table(model, create_field=field) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/schema.py", line 229, in _remake_table mapping[create_field.column] = self.quote_value( File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/schema.py", line 68, in quote_value raise ValueError( ValueError: Cannot quote parameter value [] of type <class 'list'>
错误原因
ArrayField是PostgreSQL专属字段,SQLite不支持该字段类型。- 迁移执行时会按顺序先执行0007(添加ArrayField),此时SQLite无法处理ArrayField的默认值
[](list类型),直接抛出类型引用错误。
解决步骤
1. 合并迁移操作(最直接方法)
- 删除0007和0008这两个迁移文件。
- 确保
models.py中people字段保持为CharField。 - 重新生成迁移文件:
python3 manage.py makemigrations - 执行迁移:
python3 manage.py migrate
2. 确认SQLite数据库配置
检查项目settings.py中的数据库配置,确保使用SQLite:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } }
若之前配置过PostgreSQL,替换为上述配置即可。
3. 回退已有迁移记录(若已执行过0007)
如果数据库中已经记录了0007的迁移记录,先回退到0006版本:
python3 manage.py migrate project_name 0006
之后再删除0007、0008,重新生成并执行迁移。
内容的提问来源于stack exchange,提问作者temi a
相关产品推荐
相关产品推荐

