【发布时间】:2016-08-12 03:38:21
【问题描述】:
假设我在 Laravel 中有一个父表和一个子表,对于表 Order,我的模型如下所示:
public function up()
{
Schema::create('orders', function(Blueprint $table)
{
$table->integer('customer_id')->unsigned();
$table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
$table->increments('id');
我知道 Eloquent 会将 id 视为 orders 的主键,因此会自动在该主键上创建索引。
我应该怎么做才能确保customer_id 是主键索引的一部分,按顺序设置:
1. customer_id
2. id
表格示例
Customer
+-------------+
| id | --> primary key
|- - - - - - -|
| name |
| address |
+-------------+
|
|
|
A
Order
+------------------+
| customer_id (fk) | --- primary key
| id | --- primary key
|- - - - - - - - - |
| date |
+------------------+
【问题讨论】:
-
尝试添加
$table->primary(array('id', 'customer_id'));! -
参考以下链接。我希望它会帮助你。 stackoverflow.com/questions/22477726/…
-
有什么理由需要它作为主键索引的一部分?这对我来说没有多大意义。如果您只是在寻找有效的连接,那么外键应该就足够了,您可以通过
explain select...来验证它 -
我的想法是,当我使用
customer_id查询订单表时,MySQL 通过主键索引(customer_id,id)访问orders会更快,因为customer_id将是主键索引的第一列。我在 DB2 上工作过,它就是这样做的,但我猜 MySQL 做的不一样?
标签: laravel laravel-5 eloquent