django mysql外键约束,了解/ mySQL也是Django中的外键关系

So I've inherited some django.

The mySQL table is simple enough where parent is NOT a FK relationship just the "Parent" id:

CREATE TABLE `Child` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`parent` int(10) unsigned NOT NULL,

`name` varchar(255) NOT NULL,

UNIQUE KEY `id` (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=24;

But then the originator did this..

class Child(models.Model):

"""Project Child information"""

id = models.AutoField(primary_key=True)

parent = models.ForeignKey(Parent)

name = models.CharField(max_length=255)

class Meta:

managed = False

Admittedly I am NOT a SQL Jockey but I know that a "real" Foreign Key Relationship looks similar to this notice CONSTRAINT...

CREATE TABLE `Child` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`parent_id` int(11) NOT NULL,

`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

PRIMARY KEY (`id`),

KEY `child_63f17a16` (`parent_id`),

CONSTRAINT `parent_id_refs_id_34923e1e` FOREIGN KEY (`parent_id`) REFERENCES `Parent` (`id`)

) ENGINE=InnoDB;

What I want to know is the following:

What problems could I expect to see by this "trickery".

While this appears to work - is it recommended or advised.

Would we be advised to modify the SQL to add in the constraint?

Thanks so much!

解决方案Not having an actual constraint might lead to broken references, invalid parents and other sorts of data inconsistencies. I am not a Django expert but I would venture a guess that in most cases Django will still handle the relations fine unless you purposefully add some invalid records.

Normally, if your RDBMS supports foreign key constraints, there is absolutely no reason not to use them, and it could potentially be considered a design flaw to ignore them.

You should consider adding the key constraints. Not only do they give your DBMS a good idea of how to optimize the queries, they also ensure consistency in your data. I am pretty sure Django has a setting somewhere that will automatically generate the SQL to add the key constraints when you run manage.py syncdb

For more information about why you should prefer foreign keys, you should read the MySQL Foreign Key Documentation

Most interestingly:

InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. (This is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.) index_name, if given, is used as described previously.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值