我正在为我的Web应用程序创建一个应用程序,当我尝试将模型迁移到MySQL数据库时,Django引发以下错误。
所有迁移之前都运行良好。我发现CarInstance id与主键(pk)绑定,该主键与我要用于int:pk />的简单URL架构不兼容,后者将是(showroom / car / 2)。
我试图通过简单的复制/粘贴将'id'变量移动到另一个类,该复制/粘贴将链接到CarInstance作为外键,类似于MDN在此处使用book&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)
异常错误
我希望迁移能够正常进行,但是会引发此错误。我不确定断点在哪里,所以我只在此处发布了整个代码块。
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的网址使用了难看的UUID(例如,showroom / cars / 6ec1fa4c-cc0b-44f5-b512-69ff3c831929)。而且,即使语法正确,并且它们链接到正确的ID,页面仍无法正常工作。他们抛出 404 找不到页面错误。
我正在尝试将slug方法合并到url中,但是由于原始问题,我无法对模型进行任何更改。
如果您需要任何其他信息,请告诉我。
答案 0 :(得分:0)
由于此问题对于Django / Mariadb组合来说很少见,因此我将按照以下步骤解决此问题。给以后的读者。
MariaDB [(none)]> use 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
幸运的是,该站点正在建设中,我只有三个要测试的对象。 :-)