gpt4 book ai didi

mysql - 使用外键和 Django 更新 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 13:21:31 24 4
gpt4 key购买 nike

我正在为我正在启动的数据库项目构建布局。自从我上次从事构建外键等工作以来已经有一年多了,称我生疏是轻描淡写的。

因此,对于我的数据库,我们将有一个包含车辆的表和一个位置表。每辆车都有一个 ID(VehID)、一个位置(Location)和一个名称(Title)。位置表具有以下字段:LocID,该位置所属的车辆 ID (VehID),它是来自车辆数据库的外键,它还具有车辆的纬度和经度。

表格如下所示:

-----------
| vehicle | - //Holds all vehicles
-----------
| VehID | - PK, auto_incrementing
|Locations| - //should link to most current location, with location.VehID == vehicle.VehID
| Title | - varchar(40)
-----------

-----------
|location | - //Holds all locations where a vehicle has been
-----------
|LocID | - PK, auto_incrementing
|VehID | - Foreign Key from vehicle
|Latitude | - decimal(10,6)
|Longitude| - decimal(10,6)
-----------

如果有帮助的话,我正在使用 django 框架进行 ORM。每次我尝试向位置表添加新条目时,尝试设置 VehID 时都会出错...我该如何设置?如果不能,我如何才能链接车辆和位置字段?就像我说的..已经有一段时间了。

最佳答案

这是我解决这个问题的方法:

  • 删除 vehicle.Locations(否则两个表之间会出现循环关系)
  • location添加timestamp字段,其中包含车辆到达特定位置的时间
  • 确保在location中引用vehicle之前始终先将条目插入vehicle

要查找车辆的当前位置,请对 vehicle.VehId 执行 GROUP BY 搜索,然后选择时间戳小于当前时间的那个。

关于mysql - 使用外键和 Django 更新 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778049/

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