我正试图将一个.csv文件导入到我的Django db.sqlite3中,我完全按照Youtube视频中的说明操作。但是,我的.csv文件中有两行具有相同的纬度和长度值,sqlite3无法导入某些行,因为纬度和长度值已经出现在表中
sqlite> .import venues.csv myapi_venue
venues.csv:17: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:44: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:49: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:60: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:66: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:73: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:80: INSERT failed: UNIQUE constraint failed: myapi_venue.name
我试图强迫模型将unique设置为False,但这没有帮助
class venue(models.Model):
name = models.CharField(_("name"), primary_key=True, max_length=300)
address = models.CharField(_("address"), max_length=300, unique=False)
categories = models.CharField(_("category"), null=True, max_length=300, unique=False)
latitude = models.FloatField(_("latitude"), max_length=150, unique=False)
longitude = models.FloatField(_("longitude"), max_length=150, unique=False)
当我尝试迁移更改时,会显示以下内容:
$ python manage.py makemigrations
No changes detected
如何将所有行导入数据库
因此我可以证明主键是唯一的: conflicting rows
我想我找到了它显示错误的原因。出于某种原因,模型中的主键与csv中的不同列匹配。 order of columns is messed up
因此,我最终不得不删除原始表并创建一个新表,以便列的顺序正确。遵循来自how to recreate a deleted table with django migrations的解决方案
相关问题 更多 >
编程相关推荐