删除hasOne或hasMany关联时,foreignKey是否应该设置为NULL?

发布于 2024-07-06 12:17:34 字数 273 浏览 8 评论 0原文

鉴于:

群组有很多人

,但关系是独立的(即Persons可以不属于一个Group而存在),删除Group时Persons表中的外键(即group_id)是否应该设置为0(或NULL)? 如果您不这样做,此人将尝试加入一个不存在的群体。

我问的原因是这是 Cakephp 中的默认行为。 如果将 dependent 设置为 true,它将删除关联的模型,但如果将其设置为 false,它将保持关联的模型不变。

Given :

Group hasMany Persons

but the relationship is independent (ie. Persons can exist without belonging to a Group), should the foreign key in the persons' table (ie group_id) be set to 0 (or NULL) when deleting a group? If you do not, the person will try to belong to a group that doesn't exist.

The reason I ask is that this is the default behavior in Cakephp. If you set dependent to true, it will delete the associated models, but if it's set to false it will leave the associated model untouched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为守护你 2024-07-13 12:17:34

是的,外键应设置为 NULL(或 0,如果这是您选择的“无组”值),否则您将失去引用完整性。 如果您的数据库支持它,您应该能够在框架中设置“删除时”触发器或级联规则来强制执行此操作。 CakePHP 中的行为似乎是正确的。 如果该值是相关的,则应在删除时将其删除。 如果它不相关,那么您需要提供额外的行为逻辑以采取正确的操作(在这种情况下,您希望将所有值设置为 NULL。在其他情况下,您可能希望设置为“默认”组, ETC)

Yes, the foreign keys should be set to NULL (or 0, if this is your chosen 'no group' value) or you lose referential integrity. If your database supports it, you should be able to set an 'On delete' trigger or a cascade rule in your framework to enforce this. And the behaviour in CakePHP seems correct. If the value is dependent, then it should be removed on deletion. If it isn't dependent then you need to give extra behaviour logic as to the correct action to take (in this case, you want to set all values to NULL. In other cases, you may want to set to a 'default' group, etc)

德意的啸 2024-07-13 12:17:34

总之,是的。 将外键保留在 person 表上会导致数据库内引用完整性丢失。

In a word, yes. Leaving the foreign key on the persons table would result in the loss of referential integrity within the database.

云淡月浅 2024-07-13 12:17:34

> 如果不这样做,该人将尝试加入一个不存在的组。

还有一种更糟糕的情况:将来可能会出现一个新的组 B,该组将重用已删除的组 A 的 ID。然后,所有以前 A 组的用户都将“神奇”地被纳入新的 B 组。

> If you do not, the person will try to belong to a group that does't exist.

There is also a worse scenario: in the future a new group B may appear that will reuse the id of deleted group A. Then all former A group's users will be "magically" enlisted into new group B.

昔梦 2024-07-13 12:17:34

实现两个实体独立的情况的另一种更稳定的方法是从 Person 中完全删除外键并创建一个联接表 group_persons。 这样,您在删除组时就不必担心引用完整性。 当您删除组时,关联将从 group_persons 中删除。

该表将如下所示

id, group_id, person_id

group_persons 模型将如下所示

Person hasMany GroupPerson
Group hasMany GroupPerson
GroupPerson belongsTo Person, Group

如果您希望人员一次只能属于一个组,请在 GroupPerson 中设置唯一的验证规则。

var $validate=array(
    'person_id'=>array(
        array(
        'rule'=>'isUnique',
        'message'=>'This person is already in a group.'
        )
    )
);

An alternative, more stable way to implement a situation where both entities are independent would be to remove the foreign key entirely from Person and create a join table group_persons. This way you won't have to worry about your reference integrity when deleting a group. When you delete a group, the association would be deleted from group_persons.

The table would look like this

id, group_id, person_id

The group_persons model will look like this

Person hasMany GroupPerson
Group hasMany GroupPerson
GroupPerson belongsTo Person, Group

If you want the Person to only be able to be in one group at a time, set a unique validation rule in GroupPerson.

var $validate=array(
    'person_id'=>array(
        array(
        'rule'=>'isUnique',
        'message'=>'This person is already in a group.'
        )
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文