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

标签 php mysql laravel database-migration

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

表一

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

 }

最佳答案

发生错误是因为 cat_id 在您的 users 表中不存在。除此之外,在编写 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... 
     });

 }

table 上团队

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

    });
}

关于php - 如何在 Laravel 迁移中连接 3 个表(语法错误或访问冲突),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888018/

相关文章:

mysql - 获取表中每个条目组合的公共(public)联接

php - 如何使用 PHP 和 MySQL 检索该表的分组信息?

php - Firefox 默认如何在 HTML 元素属性中显示 PHP 通知?

php - 魔法函数 __call 函数不起作用

php - 什么更快? Ajax 加载 JSON 或 Ajax 加载完成输出

PHP Foreach SQL,INSERT INTO ON DUPLICATE KEY UPDATE 仅插入最后一个查询?

php - 如何使用 cakephp find 从 mysql 表中查找共同的 friend 及其数量

php - Laravel ownsToMany 不返回结果

php - Laravel Eloquent leftJoin 在 whereIn 查询中

php - 您不能在 laravel 中序列化或反序列化 PDO 实例