我认为,在生成简单的数据透视表(多对多)表时,(默认情况下)雄辩会强制执行两个id列的唯一性?
或者我做错了什么?
预约模式
public function client()
{
return $this->belongsToMany('App\Client')->withTimestamps();
}
客户端模型
public function appointment()
{
return $this->belongsToMany('App\Appointment')->withTimestamps();
}
移民
$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();
如果eloquent默认不这样做,我将不得不手动添加索引:
$table->unique(array('appointment_id', 'client_id'));
1 回答
Eloquent不能强制执行两个id列的唯一性,因为Eloquent不负责数据库架构/配置 .
迁移负责这些内容,因此您必须在迁移中定义唯一性 .