dqxmf02844 2017-12-19 13:23
浏览 70

如何在Laravel迁移中连接3个表(语法错误或访问冲突)

I have limited knowledge of Laravel, PHP and MySql. I am trying to set up a database with 3 connected tables, using foreign keys. I did this manually in WorkBench MySql before, but I can't make it in Laravel. Here is my error and my code.

SQL STATE[42000]: Syntax error or access violation: 1072 Key column 'cat_id  
  ' doesn't exist in table (SQL: alter table `users` add index `users_cat_id  
  _usr_id_index`(`cat_id`, `usr_id`)) 

Table 1

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('usr_id')->autoIncrement();
        $table->string('usr_email',45)->unique();
        $table->string('usr_password',256);
        $table->rememberToken();
        $table->string('usr_name',45);
        $table->string('usr_surname',45);
        $table->string('usr_title',45);
        $table->string('usr_psc',5);
        $table->string('usr_region',128);
        $table->string('usr_adress',128);
        $table->string('usr_tel',10);
        $table->string('usr_tel_int',13);
        $table->binary('usr_image');
        $table->tinyInteger('usr_type');
        $table->index(['cat_id','usr_id'])->nullable();
        $table->index(['tem_id','usr_id'])->nullable();

    });
}

Table 2

public function up()
{
     Schema::create('category', function (Blueprint $table) {
         $table->primary('cat_id');
         $table->string('cat_name',45);
     });
     Schema::table('category', function(Blueprint $table) {
         $table->foreign('cat_id')->references('cat_id')->on('users');
      });
 }

Table 3

public function up()
{
     Schema::create('team', function (Blueprint $table) {
       $table->increments('tem_id');
       $table->unique('tem_sub_id');
       $table->string('tem_name',45);
       $table->string('tem_subteam_name',45);

     });
     Schema::table('team', function(Blueprint $table) {
         $table->foreign('tem_id')->references('tem_id')->on('users');
      });

 }
  • 写回答

1条回答 默认 最新

  • dqu3974 2017-12-19 14:04
    关注

    The error occurs because cat_id does not exist in your users table. Apart from that, there are some points to keep in mind when writing Laravel migrations:

    • Unless you set FOREIGN_KEY_CHECKS to 0, you can only reference to existing tables, that is why I changed the order of creating your tables in my examples below.

    • Keep in mind that, in order to make Laravel magic methods work, you should write your table names in plural form (users, teams, categories).

    • Use 'id' for your id's (instead of 'usr_id'). Try to avoid using your table name as a prefix for your database columns.

    Because Laravel Eloquent ORM heavily relies on standards, I would suggest to rewrite your tables in a fashion like I did below, following Laravel Eloquent conventions:

    Table categories

    public function up()
    {
         Schema::create('categories', function (Blueprint $table) {
             $table->increments('id');
    
             $table->string('name',45);
             // etc... 
         });
    
     }
    

    Table teams

    public function up()
    {
         Schema::create('teams', function (Blueprint $table) {
           $table->increments('id');
    
           $table->string('name',45);
           // etc... 
    
         });
     }
    

    Table users

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned()->nullable();;
            $table->foreign('category_id')->references('id')->on('categories');
            $table->integer('team_id')->unsigned()->nullable(); 
            $table->foreign('team_id')->references('id')->on('teams');
    
            $table->string('email',45)->unique();
            // etc...
    
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。