gpt4 book ai didi

mysql - 如何在 Django 中修复 "Truncated incorrect INTEGER value: ' UUID'"

转载 作者:行者123 更新时间:2023-11-29 15:36:47 26 4
gpt4 key购买 nike

我正在为我的网络应用程序创建一个应用程序,当我尝试将模型迁移到 MySQL 数据库时,Django 引发了如下所示的错误。

所有迁移之前都运行良好。我发现 CarInstance id 与主键 (pk) 绑定(bind),这与我要使用的 int:pk/> 的简单 url 模式不兼容,即 (showroom/car/2)。

我尝试通过简单的复制/粘贴将“id”变量移动到另一个类,该类将作为外键链接到 CarInstance,类似于 MDN 对其书籍和 BookInstance 模型所做的操作。我认为这将有助于创建一个单独的主键,但我在这里。 :-)

urls.py

    path('', views.index, name='index'),
path('cars/', views.CarListView.as_view(), name="cars"),
path('car/<slug:uuid>/', views.CarDetailView.as_view(), name="car-detail"),
],

Models.py代码(省略其他代码):

class CarInstance(models.Model):
manufacturer = models.ForeignKey('Manufacturer', on_delete=models.SET_NULL, null=True)
car_model = models.CharField('Model', max_length=50, null=True)
description = models.TextField(max_length=4000)
vin = models.CharField('VIN', max_length=17, help_text='Enter the 17 character VIN number.', blank=True, null=True)
mileage = models.IntegerField(verbose_name='Mileage')
car_images = models.ImageField(help_text='Upload pictures', upload_to=image_directory_path, storage=image_storage)
date_added = models.DateField(auto_now_add=True)
engine_displacement = models.CharField(default=2.0, max_length=3, help_text="Engine displacement in Liters (E.g. 2.0, 4.2, 6.3)")
price = models.IntegerField(default=0)

id = models.UUIDField(primary_key=True, default=uuid.uuid4,
help_text="Unique ID for this car.", editable=False)

异常错误

我预计迁移会正常进行,但它却抛出了此错误。我不确定断点在哪里,所以我只是在这里发布了整个 block 。

C:\code\torque>py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, showroom
Running migrations:
Applying showroom.0012_auto_20190930_0701...Traceback (most recent call last):
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\connections.py", line 226, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1292, "Truncated incorrect INTEGER value: '6ec1fa4ccc0b44f5b51269ff3c831929 '")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\base\schema.py", line 535, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\base\schema.py", line 685, in _alter_field
params,
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\base\schema.py", line 137, in execute
cursor.execute(sql, params)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\Envs\torque\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\USER\Envs\torque\lib\site-packages\MySQLdb\connections.py", line 226, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1292, "Truncated incorrect INTEGER value: '6ec1fa4ccc0b44f5b51269ff3c831929 '")

请注意,id 末尾 (29) 和右引号之间存在巨大间隙

更新:

过去几个小时我一直在处理这个问题,但仍然无法进行迁移。我发现特定 CarInstances 的 url 使用 UUID(例如 showroom/cars/6ec1fa4c-cc0b-44f5-b512-69ff3c831929),这是难看的。而且,即使语法是正确的,并且它们链接到正确的 ID,页面仍然无法工作。他们抛出 404 页面未找到错误。

我正在尝试将 slug 方法合并到 URL 中,但由于最初的问题,我无法对模型进行任何更改。

如果您需要任何其他信息,请告诉我。

最佳答案

由于这个问题对于 Django/Mariadb 组合来说很少见,因此以下是我为解决该问题而采取的步骤。供 future 的读者使用。

  1. 打开 MySQL 客户端命令。
  2. 输入了我的密码,然后更改了我的数据库

MariaDB [(none)]>使用torquedb

  • 删除了数据库,并创建了一个新数据库。
  • MariaDB [torquedb]> DROP database torquedb;
    Query OK, 12 rows affected (0.089 sec)

    MariaDB [(none)]> CREATE database torquedb
    -> ;
    Query OK, 1 row affected (0.001 sec)

    MariaDB [(none)]> show tables
    -> ;
    ERROR 1046 (3D000): No database selected
    MariaDB [(none)]> use torquedb
    Database changed


  • 然后我重新运行迁移
  • (torque) C:\code\torque> py manage.py makemigrations
    torque) C:\code\torque> py manage.py migrate

    幸运的是,该网站正在 build 中,我只有三个对象可供测试。 :-)

    关于mysql - 如何在 Django 中修复 "Truncated incorrect INTEGER value: ' UUID'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58163665/

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