如何在 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"
最佳答案
您混淆了 on
和 references
方法。
正确的变体是:
$table
->foreign(['profile_id', 'app_id'])
->references(['profile_id', 'app_id'])
->on('profile_apps');
关于php - Laravel 中的复合外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866928/