php - Laravel 中的复合外键

标签 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_apps')->on(['profile_id', 'app_id']);

变成:

"ErrorException : Array to string conversion" in C:\Users\CAM\Projects\mcr-back\vendor\laravel\framework\src\Illuminate\Database\Grammar.php:39

与字符串相同,

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

有错误:

"SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'profile_id, app_id' doesn't exist in table"

最佳答案

您混淆了 onreferences 方法。

正确的变体是:

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

关于php - Laravel 中的复合外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866928/

相关文章:

php - 连接 postgres 和 laravel

php - 将对象转换为数组 PHP - 仅公共(public)字段

php - 网站链接在 google chrome 上不起作用,但在 firefox 上工作正常

php - 在 while (mysql) 中添加 session

php - 两个ajax请求可能发生冲突?

php - Laravel 基本身份验证 - 获取用户详细信息

php - 如何在 Laravel 中保存多态关系?

MySQL 错误 1215 : Cannot add foreign key constraint

mysql - 使用拼写错误的列名执行外键查询没有问题

mysql - 我一直收到 SQL 语法错误,我不知道为什么