将 id 列添加到 Laravel 中的数据透视表有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34274998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:49:04  来源:igfitidea点击:

Any advantages of adding an id column to a pivot table in Laravel?

laravellaravel-4laravel-5

提问by user3489502

Is there any advantages of having an id column in a pivot table (many to many relationship) in Laravel (i'm using version 5.1)?

在 Laravel(我使用的是 5.1 版)的数据透视表(多对多关系)中有一个 id 列有什么好处吗?

With an id

带身

        $table->increments('id');

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();

Without an id

没有身

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();

采纳答案by Kryten

In general, the answer is no, provided that Laravel's Eloquent models are managing the relationship.

一般来说,答案是否定的,前提是 Laravel 的 Eloquent 模型正在管理这种关系。

If, however, you need to access the tables from outside of Eloquent models (say, from another application or in the distant future when you rewrite your application to use the next big framework), an ID will come in handy.

但是,如果您需要从 Eloquent 模型外部访问表(例如,从另一个应用程序或在遥远的将来重写您的应用程序以使用下一个大框架时),ID 将派上用场。

回答by wasthishelpful

Late answer, but you can remove the auto increment id, and use your foreign keys as a composite primary key for your pivot table:

迟到的答案,但您可以删除 auto increment id,并将外键用作数据透视表的复合主键:

$table->integer('appointment_id')->unsigned();
$table->foreign('appointment_id')->
    references('id')->on('appointments')->onDelete('cascade');

$table->integer('client_id')->unsigned();
$table->foreign('client_id')->
    references('id')->on('clients')->onDelete('cascade');

// add the following instruction

$table->primary(['appointment_id', 'client_id']);

Tested with Laravel 5.6, this works without breaking Eloquent's management while ensuring the uniqueness of the associations directly in your database structure.

使用 Laravel 5.6 进行测试,这不会破坏 Eloquent 的管理,同时确保直接在数据库结构中关联的唯一性。

More about creating indexes...

有关创建索引的更多信息...

回答by Nikolay Antonov

If you are going to set up MySQL Group Replication - you have to have the Primary key on every table.

如果您要设置 MySQL 组复制 - 您必须在每个表上都有主键。

17.7.1 Group Replication Requirements Server instances that you want to use for Group Replication must satisfy the following requirements.

Infrastructure InnoDB Storage Engine. Data must be stored in the InnoDB transactional storage engine. Transactions are executed optimistically and then, at commit time, are checked for conflicts. If there are conflicts, in order to maintain consistency across the group, some transactions are rolled back. This means that a transactional storage engine is required. Moreover, InnoDB provides some additional functionality that enables better management and handling of conflicts when operating together with Group Replication.

Primary Keys. Every table that is to be replicated by the group must have a defined primary key, or primary key equivalent where the equivalent is a non-null unique key. Such keys are required as a unique identifier for every row within a table, enabling the system to determine which transactions conflict by identifying exactly which rows each transaction has modified.

IPv4 Network. The group communication engine used by MySQL Group Replication only supports IPv4. Therefore, Group Replication requires an IPv4 network infrastructure.

Network Performance. Group Replication is designed to be deployed in a cluster environment where server instances are very close to each other, and is impacted by both network latency as well as network bandwidth.

17.7.1 组复制要求 要用于组复制的服务器实例必须满足以下要求。

基础设施 InnoDB 存储引擎。数据必须存储在 InnoDB 事务存储引擎中。事务以乐观的方式执行,然后在提交时检查冲突。如果有冲突,为了保持整个组的一致性,一些事务会被回滚。这意味着需要一个事务存储引擎。此外,InnoDB 提供了一些附加功能,可以在与 Group Replication 一起操作时更好地管理和处理冲突。

主键。组要​​复制的每个表都必须有一个定义的主键,或等效的主键,其中等效键是非空的唯一键。这些键需要作为表中每一行的唯一标识符,使系统能够通过准确识别每个事务修改了哪些行来确定哪些事务发生冲突。

IPv4 网络。MySQL Group Replication 使用的组通信引擎仅支持 IPv4。因此,组复制需要 IPv4 网络基础设施。

网络性能。Group Replication 旨在部署在服务器实例彼此非常接近的集群环境中,并受网络延迟和网络带宽的影响。

回答by shempignon

Be careful while adding an id as primary key on pivot tables as it will "break" any validation on the foreign key couple unicity, meaning you could have multiple records having the same foreign keys couple.

在数据透视表上添加 id 作为主键时要小心,因为它会“破坏”外键对唯一性的任何验证,这意味着您可能有多个具有相同外键对的记录。