gpt4 book ai didi

python - Django - 旧版 MySQL 数据迁移错误

转载 作者:行者123 更新时间:2023-11-29 12:14:37 25 4
gpt4 key购买 nike

我在 models.py 文件中 inspectdb 中的外键定义方面遇到了许多问题。

我有一组旧表,我想将其合并到我的 Django 应用程序中,因此我运行了 python manage.pyspectdb > models.py

我想在我的应用程序中使用 7 个 GPS 表。查看其中 2 个就足以描述我遇到的问题。

MySQL建表语句:

CREATE TABLE `gps` (
`longitude_dir` char(1) DEFAULT NULL,
`longitude` decimal(8,5) DEFAULT NULL,
`latitude` decimal(7,5) DEFAULT NULL,
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`host_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`latitude_dir` char(1) DEFAULT NULL,
`host` varchar(24) NOT NULL,
`altitude_units` char(1) DEFAULT NULL,
`gps_time` datetime DEFAULT NULL,
`raw` blob NOT NULL,
`clean` bit(1) DEFAULT NULL,
`data_valid` bit(1) DEFAULT NULL,
`modem` varchar(15) NOT NULL,
`altitude` decimal(7,2) DEFAULT NULL,
PRIMARY KEY (`id`,`host`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

CREATE TABLE `gps_zda` (
`local_zone_minutes` tinyint(4) DEFAULT NULL,
`host` varchar(24) NOT NULL,
`year` tinyint(4) DEFAULT NULL,
`day` tinyint(4) DEFAULT NULL,
`month` tinyint(4) DEFAULT NULL,
`local_zone` tinyint(4) DEFAULT NULL,
`gps_id` int(10) unsigned NOT NULL,
`timestamp` time DEFAULT NULL,
KEY `gps_fk_zda` (`gps_id`,`host`),
CONSTRAINT `gps_fk_zda` FOREIGN KEY (`gps_id`, `host`) REFERENCES `gps` (`id`, `host`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

这是模型的子集

from __future__ import unicode_literals

from django.db import models


class Gps(models.Model):
longitude_dir = models.CharField(max_length=1, blank=True, null=True)
longitude = models.DecimalField(max_digits=8, decimal_places=5, blank=True, null=True)
latitude = models.DecimalField(max_digits=7, decimal_places=5, blank=True, null=True)
id = models.AutoField()
host_time = models.DateTimeField()
latitude_dir = models.CharField(max_length=1, blank=True, null=True)
host = models.CharField(max_length=24)
altitude_units = models.CharField(max_length=1, blank=True, null=True)
gps_time = models.DateTimeField(blank=True, null=True)
raw = models.TextField()
clean = models.TextField(blank=True, null=True) # This field type is a guess.
data_valid = models.TextField(blank=True, null=True) # This field type is a guess.
modem = models.CharField(max_length=15)
altitude = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)

class Meta:
managed = False
db_table = 'gps'
unique_together = (('id', 'host'),)

class GpsZda(models.Model):
local_zone_minutes = models.IntegerField(blank=True, null=True)
host = models.ForeignKey(Gps, db_column='host')
year = models.IntegerField(blank=True, null=True)
day = models.IntegerField(blank=True, null=True)
month = models.IntegerField(blank=True, null=True)
local_zone = models.IntegerField(blank=True, null=True)
gps = models.ForeignKey(Gps)
timestamp = models.TimeField(blank=True, null=True)

class Meta:
managed = False
db_table = 'gps_zda'
<小时/>

1.因此,我在尝试 python manage.py makemigrations 时遇到的第一个问题是 (gps.id, gps.host) 没有正确定义为主键。事实上,似乎很多限制都被忽略了。我一开始收到一个错误,说 gps.id 被定义了两次。

改变

id = models.Autofield()
...
host = models.CharField(max_length=24)

id = models.Autofield(primary_key=True)
...
host = models.CharField(primary_key=True, max_length=24)

让 django 知道不要创建它自己的 id 字段。我仍然不确定它是否正确处理共享 key 。

<小时/>

2.尝试运行 makemigrations 时出现了第二个问题,因为 gps_zda 表具有共享的、唯一的外键。 Django 提示它不知道如何命名第二个外键。

向其中一个外键添加 lated_name 似乎可以阻止投诉。

我变了

host = models.ForeignKey(Gps, db_column='host')

host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')

我再次不确定这是否正确处理共享外键。

<小时/>

3.为了进行一些测试,我决定将模型添加到 admin.py 并查看字段是否正确填充。

这导致发现更多问题。

问题 3。当我打开 gps 对象 时,我看到位字段(gps.clean、gps.data_valid)已转换为 TextFields。我尝试通过简单地将 TextField 更改为 NullBooleanField 来快速修复这些问题。现在,在管理页面上,我看到一个适当的下拉菜单,其中包含选项:未知、是、否。不幸的是,现有的位值 1 被解析为未知。

我不知道如何解决这个问题,尝试更改数据库结构会出现问题。

<小时/>

4.再次在管理中,我无法正确加载 gps_zda 对象。起初,我收到一个错误,提示 gps_zda.id 列不存在。我能够通过改变来解决这个问题

host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')
...
gps = models.ForeignKey(Gps)

host = models.ForeignKey(Gps, primary_key=True, db_column='host', related_name='gps_zda_host_fk')
...
gps = models.ForeignKey(Gps, primary_key=True)

这让 Django 知道不要寻找 gps_zda.id 字段。

我还在 GpsZda 的 Meta 类中添加了 `unique_together = (('gps', 'host'),)。

但是,我仍然无法通过管理页面查看 gps_zda 数据。我收到以下错误 主键 u'Arthur' 的 gps zda 对象不存在。 其中 Arthur 是有效的 gps.host 值。

<小时/>

当我开始研究 Django 时,我认为它会很有趣。没那么多。我真的很怀疑将任何现有数据库转移到 Django 项目中是否可行。

这是我当前的模型。任何关于如何让它们更好地与我的 table 配合使用的建议都将受到欢迎。

class Gps(models.Model):
longitude_dir = models.CharField(max_length=1, blank=True, null=True)
longitude = models.DecimalField(max_digits=8, decimal_places=5, blank=True, null=True)
latitude = models.DecimalField(max_digits=7, decimal_places=5, blank=True, null=True)
id = models.AutoField(primary_key=True)
host_time = models.DateTimeField(primary_key=True)
latitude_dir = models.CharField(max_length=1, blank=True, null=True)
host = models.CharField(primary_key=True, max_length=24)
altitude_units = models.CharField(max_length=1, blank=True, null=True)
gps_time = models.DateTimeField(blank=True, null=True)
raw = models.TextField()
clean = models.NullBooleanField(null=True) # This field type is a guess.
data_valid = models.NullBooleanField(null=True) # This field type is a guess.
modem = models.CharField(max_length=15)
altitude = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)

class Meta:
managed = False
db_table = 'gps'
unique_together = (('id', 'host'),)

class GpsZda(models.Model):
local_zone_minutes = models.IntegerField(blank=True, null=True)
host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')
year = models.IntegerField(blank=True, null=True)
day = models.IntegerField(blank=True, null=True)
month = models.IntegerField(blank=True, null=True)
local_zone = models.IntegerField(blank=True, null=True)
gps = models.ForeignKey(Gps, primary_key=True, unique=True)
timestamp = models.TimeField(blank=True, null=True)

class Meta:
managed = False
db_table = 'gps_zda'
unique_together = (('gps', 'host'),)

最佳答案

Django 目前不支持表中的复合主键。 (参见 wiki page 和缺少 model meta options 这是定义它的地方。)

最好的选择可能是向您的 gps 表中添加另一个标识列,以便 Django 可以正常使用它。

对于其他问题,最好研究并针对它们发布单独的问题。

关于python - Django - 旧版 MySQL 数据迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30039002/

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