Php Laravel中的复合外键

Php Laravel中的复合外键,php,laravel,foreign-keys,migration,Php,Laravel,Foreign Keys,Migration,如何在Laravel迁移中编写此约束 ALTER TABLE user_profiles ADD CONSTRAINT app_profiles FOREIGN KEY (profile_id, app_id) REFERENCES profile_apps (profile_id, app_id); 我试过这个: $table->foreign(['profile_id', 'app_id'])->references('profile_a

如何在Laravel迁移中编写此约束

ALTER TABLE user_profiles 
   ADD CONSTRAINT app_profiles 
       FOREIGN KEY (profile_id, app_id) 
       REFERENCES profile_apps (profile_id, app_id);
我试过这个:

$table->foreign(['profile_id', 'app_id'])->references('profile_apps')->on(['profile_id', 'app_id']);
这就变成了:

中的“ErrorException:数组到字符串转换” C:\Users\CAM\Projects\mcr back\vendor\laravel\framework\src\illumb\Database\Grammar.php:39

和弦一样

$table->foreign('profile_id, app_id')->references('profile_apps')->on('profile_id, app_id');
有误:

“SQLSTATE[42000]:语法错误或访问冲突:1072键列 “配置文件id,应用程序id”在表中不存在

用这种方法

注意:确保在
数据库>迁移中
配置文件表和
应用程序表
位于该
表迁移上方

2014_06_14_23123123_创建_配置文件_表格

2014年6月14日23123日创建应用程序表

以前

2014年6月14日23123日创建此表格


您正在使用将
引用方法混淆

右变体是:

$table
    ->foreign(['profile_id', 'app_id'])
    ->references(['profile_id', 'app_id'])
    ->on('profile_apps');

顺便说一句,ALTER语句在控制台中运行良好。我已经设置了约束,这些约束与我正在请求的约束不同。需要的参考是profile_apps,这是一个不同的表,意味着某些配置文件属于某些(但不是全部)应用程序。用户分配的配置文件应该存在于该表中。您是指多对多关系吗?或多态关系。我建议你试试这个,请把问题再读一遍。我没有要求任何与关系相关的东西,而是要求迁移中的外键约束。这很简单:我可以在SQL命令行中完成我想在Laravel迁移中完成的事情。对!非常感谢。
$table
    ->foreign(['profile_id', 'app_id'])
    ->references(['profile_id', 'app_id'])
    ->on('profile_apps');