我有一个项目,我在命令提示符的第一个调用这两个命令: p> \ n
php artisan make:model音乐-m
php artisan make:model Artist -m
code> pre>
然后我创建一个外键 像这样的音乐迁移文件: p>
public function up()
{
Schema :: create('musics',function(Blueprint $ table){
$ table - >增量('id');
$ table->整数('artist_id') - > unsigned();
$ table-> timestamps();
$ table-> foreign( 'artist_id') - >引用('id') - > on('artists');
});
}
code> pre>
现在当 我运行迁移命令,如: p>
php artisan migrate
code> pre>
但我有一个错误: p >
Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table`musics`添加约束`musics_artist_id_foreign`外键(`artist_id` ) 在删除级联上引用`artists`(`id`)
在D:\ sites \ laravel \ MrMusic \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:664
660 | //如果在尝试运行查询时发生异常,我们将格式化错误
661 | //包含SQL绑定的消息,这将使此异常成为
662 | //对开发人员更有帮助,而不仅仅是数据库的错误。
663 | catch(Exception $ e){
> 664 | 抛出新的QueryException(
665 | $ query,$ this-> prepareBindings($ bindings),$ e
666 |);
667 | }
668 |
异常跟踪:
1 PDOException ::(“SQLSTATE [HY000]:常规错误:1215无法添加外键约束”)
D:\ sites \ laravel \ MrMusic \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:458
2 PDOStatement :: execute()
D:\ sites \ laravel \ MrMusic \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:458
请使用参数-v查看更多详细信息。
code> pre>
注意: strong>我将测试一个 几种方法,比如设置两个字段的索引,...... p>
div>