我已经将网站的开发转移到使用 Docker。我用 postgresql 替换了 sqlite 作为我的数据库,然后在我的 Docker 环境中运行命令
docker-compose exec web python manage.py migrate
,它产生了以下错误:
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.CannotCoerce: cannot cast type bigint to uuid
LINE 1: ...quirementschat" ALTER COLUMN "id" TYPE uuid USING "id"::uuid
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/code/manage.py", line 22, in <module>
main()
File "/code/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 106, in wrapper
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 356, in handle
post_migrate_state = executor.migrate(
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 135, in migrate
state = self._migrate_all_forwards(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
state = self.apply_migration(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
state = migration.apply(state, schema_editor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/migrations/migration.py", line 132, in apply
operation.database_forwards(
File "/usr/local/lib/python3.11/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 830, in alter_field
self._alter_field(
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 287, in _alter_field
super()._alter_field(
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 1055, in _alter_field
self.execute(
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 48, in execute
return super().execute(sql, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 201, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 102, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: cannot cast type bigint to uuid
LINE 1: ...quirementschat" ALTER COLUMN "id" TYPE uuid USING "id"::uuid
^
我还没有找到任何可行的解决方案。我重命名了“id”字段并运行了迁移,但最终仍然出现相同的错误。我删除了数据库中的所有记录,将字段从 Charfield 更改为 UUIDField 但错误仍然存在。
这是我的模型.py:
class RequirementsChat(models.Model):
id = models.CharField(primary_key=True, max_length=38)
#id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, max_length=37)
alias = models.CharField(max_length=20, blank=True, null=True)
email = models.CharField(max_length=80, blank=True, null=True)
language = models.CharField(max_length=10, blank=True, null=True)
due_date = models.CharField(max_length=10, blank=True, null=True)
subtitle_type = models.CharField(max_length=10, blank=True, null=True)
transcript_file_type = models.CharField(max_length=10, blank=True, null=True)
additional_requirements = models.TextField(max_length=500, blank=True, null=True)
date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
url = models.CharField(max_length=250, blank=True, null=True)
task_completed = models.BooleanField(default=False)
class UploadedFile(models.Model):
input_file = models.FileField(upload_to='files')
chat_id = models.CharField(max_length=33, null= True)
requirements_chat = models.ForeignKey(RequirementsChat, on_delete=models.CASCADE, related_name='files', null=True)
有什么建议吗?
你必须
对于步骤1
class RequirementsChat(models.Model):
id_temp = models.UUIDField(default=uuid.uuid4, unique=True)
###
然后你必须进行迁移
python manage.py makemigrations
现在第2步
from django.db import migrations
def copy_id_to_id_temp(apps, schema_editor):
RequirementsChat = apps.get_model('yourappname', 'RequirementsChat')
for chat in RequirementsChat.objects.all():
chat.id_temp = uuid.UUID(chat.id)
chat.save()
class Migration(migrations.Migration):
dependencies = [
('yourappname', 'previous_migration'),
]
operations = [
migrations.RunPython(copy_id_to_id_temp),
]
使用
python manage.py migrate
运行迁移
步骤3
class RequirementsChat(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True)
###
最后一次做最后的
makemigrations
和migrate