dousao6260
2018-05-20 19:10
采纳率: 0%
浏览 130
已采纳

无法使用Mysql在Laravel中创建外键

I have a project and i have call these 2 commands at the first in my command prompt:

php artisan make:model Music -m
php artisan make:model Artist -m

Then i create a foreign key in music migration file like this :

public function up()
{
    Schema::create('musics', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('artist_id')->unsigned();
        $table->timestamps();
        $table->foreign('artist_id')->references('id')->on('artists');
    });
}

Now when i run migrate command like :

php artisan migrate

But i have a this error :

       Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `musics` add constraint `musics_artist_id_foreign` foreign key (`artist_id`) references `artists` (`id`) on delete cascade)

  at D:\sites\laravel\MrMusic\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      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

  Please use the argument -v to see more details.

Note : i will test a few ways like set index to both of fields,...

图片转代码服务由CSDN问答提供 功能建议

我有一个项目,我在命令提示符的第一个调用这两个命令: \ n

  php artisan make:model音乐-m 
php artisan make:model Artist -m 
   
 
 

然后我创建一个外键 像这样的音乐迁移文件:

  public function up()
 {
 Schema :: create('musics',function(Blueprint $ table){
 $ table  - >增量('id'); 
 $ table->整数('artist_id') - > unsigned(); 
 $ table-> timestamps(); 
 $ table-> foreign(  'artist_id') - >引用('id') - > on('artists'); 
}); 
} 
   
 
 

现在当 我运行迁移命令,如:

  php artisan migrate 
   
 
 

但我有一个错误:

  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查看更多详细信息。
   
 
 

注意:我将测试一个 几种方法,比如设置两个字段的索引,......

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • doulu6929 2018-05-31 15:11
    最佳回答

    I dont't have any mistake in create migrations file.

    My mistake is in create function for realation in Artist model:

    In my Artist.php i used :

    public function music(){ ... }
    

    Now i forget to set like this :

    public function musics(){ ... }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题