Mar*_*1ni 2 mysql django inspectdb
所以我只测试了一件东西,制作了下表。
# Dump of table driverclass
# ------------------------------------------------------------
CREATE TABLE `driverclass` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
# Dump of table event
# ------------------------------------------------------------
CREATE TABLE `event` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
# Dump of table driver
# ------------------------------------------------------------
CREATE TABLE `driver` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
# Dump of table driver_driverclass_event
# ------------------------------------------------------------
CREATE TABLE `driver_driverclass_event` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`driver_id` int(11) unsigned DEFAULT NULL,
`event_class_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `driver_id` (`driver_id`),
KEY `event_class_id` (`event_class_id`),
CONSTRAINT `driver_driverclass_event_ibfk_2` FOREIGN KEY (`event_class_id`) REFERENCES `driverclass_event` (`id`),
CONSTRAINT `driver_driverclass_event_ibfk_1` FOREIGN KEY (`driver_id`) REFERENCES `driver` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
# Dump of table driverclass_event
# ------------------------------------------------------------
CREATE TABLE `driverclass_event` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`event_id` int(11) unsigned DEFAULT NULL,
`driverclass_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `event_id` (`event_id`),
KEY `driverclass_id` (`driverclass_id`),
CONSTRAINT `driverclass_event_ibfk_2` FOREIGN KEY (`driverclass_id`) REFERENCES `driverclass` (`id`),
CONSTRAINT `driverclass_event_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
哪个应该是关系ManyToManyField。但是,将Djangos inspectdb其视为带有外键吨的5种型号。不Djangos inspectdb考虑ManyToManyFields,还是我的数据库模型错误?
您的数据库架构似乎还不错。但是,Django不会像您发现的那样自动创建“多对多”字段。driverclass_event和** driver_driverclass_event ** 的模型是所谓的“直通”模型。通常,您还将在关系的两端定义ManyToManyField并指定正确的“直通”模型:
class Event(db.Model):
pass
class DriverClass(db.Model):
events = db.ManyToManyField(Event, through='DriverClassEvent')
class DriverClassEvent(db.Model):
driver_class = db.ForeignKeyField(DriverClass)
event = db.ForeignKeyField(Event)
Run Code Online (Sandbox Code Playgroud)
另请参阅:https : //docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships
通常,当您要存储有关关系的信息(例如关系的开始日期或使用外键引用此关系)时,将使用贯穿模型。在您的示例中,存储哪些驱动程序与DriverClassEvent有关系。因此,DriverClassEvent应该具有一个能够引用它的模型。
class DriverDriverClassEvent(db.Model):
driver = db.ForeignKeyField(Driver)
driver_class_event = db.ForeignKeyField(DriverClassEvent)
class Driver(db.Model):
driver_class_events = db.ManyToManyField(DriverClassEvent, through='DriverDriverClassEvent')
Run Code Online (Sandbox Code Playgroud)
现在,您可以删除DriverDriverClassEvent,因为您不存储有关该关系的任何数据,或者在其他模型中引用该关系。因此,最后一个示例变为*:
class Driver(db.Model):
driver_class_events = db.ManyToManyField(DriverClassEvent, db_table='driver_driverclass_event')
Run Code Online (Sandbox Code Playgroud)
*请注意,您无法控制字段名称,因此它们必须与Django的自动生成的字段名称匹配。
| 归档时间: |
|
| 查看次数: |
1331 次 |
| 最近记录: |