我正在尝试在我的字段中进行此更改以正确更新,以便我可以处理项目的其余部分。这是当前导致问题的模型:
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)
# the people field is the issue rn
def __str__(self):
return self.name + ", " + str(self.id)
以前,people 字段是 ArrayField,但我将其更改为 CharField,因为它不需要是数组。
这是迁移 7,我将 people 字段添加为 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),
),
]
这是迁移 8,我将其更改为 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),
),
]
我已经多次删除/注释了人员字段并将其放回去,当我这样做时
python3 manage.py makemigrations
,这些更改会被检测到。但是当我这样做时 python3 manage.py migrate
它给了我这个错误:
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'>
我觉得这可能是由于 postgres 和 sqlite 在某处发生冲突造成的,但如果是这样,我不知道如何向软件表明我坚持使用 sqlite。我知道 ArrayField 是 postgres 的一部分,而不是 sqlite,所以希望这是一个开始
如果其他人也遇到类似的问题,我已经用一种非常简单的方式解决了这个问题。注释掉模型中的所有代码以及整个代码中模型的每个实例。做了
python3 manage.py makemigrations
和python3 manage.py migrate
。然后我取消所有注释并再次运行这些命令