【问题标题】:How to connect 3 tables in Laravel migrations (Syntax error or access violation)如何在 Laravel 迁移中连接 3 个表(语法错误或访问冲突)
【发布时间】:2018-06-01 22:48:20
【问题描述】:

我对 Laravel、PHP 和 MySql 的了解有限。我正在尝试使用外键建立一个包含 3 个连接表的数据库。我之前在 WorkBench MySql 中手动完成了此操作,但在 Laravel 中无法完成。这是我的错误和代码。

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`)) 

表 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();

    });
}

表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');
      });
 }

表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');
      });

 }

【问题讨论】:

  • 如果您在正确创建迁移时遇到问题,我建议您使用laravelsd.com,它是生成迁移文件的在线工具

标签: php mysql laravel database-migration


【解决方案1】:

发生错误是因为您的users 表中不存在cat_id。除此之外,在编写 Laravel 迁移时需要牢记以下几点:

  • 除非您设置FOREIGN_KEY_CHECKS to 0,否则您只能引用现有表,这就是为什么我在下面的示例中更改了创建表的顺序。

  • 请记住,为了使 Laravel 魔法方法起作用,您应该以复数形式(用户、团队、类别)编写表名。

  • 使用“id”作为您的 id(而不是“usr_id”)。尽量避免使用表名作为数据库列的前缀。

因为 Laravel Eloquent ORM 严重依赖标准,我建议按照下面Laravel Eloquent conventions 的方式重写您的表格:

表格类别

public function up()
{
     Schema::create('categories', function (Blueprint $table) {
         $table->increments('id');

         $table->string('name',45);
         // etc... 
     });

 }

桌队

public function up()
{
     Schema::create('teams', function (Blueprint $table) {
       $table->increments('id');

       $table->string('name',45);
       // etc... 

     });
 }

表用户

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...

    });
}

【讨论】:

  • 我在第一篇文章之前尝试过这个。这是代码修改后的错误。 SQLSTATE[42000]:语法错误或访问冲突:1072 表中不存在键列“cat_id”(SQL:alter table users 添加索引 users_cat_id _index(cat_id))
  • @SnatchIT ,我更新了我的答案。希望这可以帮助。为了专注于您的问题/疑问,为了清楚起见,我省略了您的一些代码。
  • 我改变了我的代码,仍然没有在 Laravel 中构建数据库。 SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQ L: alter table users add constraintusers_cat_id_foreign foreign key (cat_id) 引用categories (id)) 在Connection.php 行458: SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束
  • 我复制/粘贴并再次出现类似错误。如果这有什么不同,我会在 Homestead 上跑步。无论如何,请您帮忙,我将再次重写我的代码。
  • 宅基地很好。
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 2023-02-10
  • 2018-02-06
  • 2021-02-08
  • 2020-08-17
  • 2019-02-10
  • 2014-01-07
  • 2017-04-16
相关资源
最近更新 更多