当前位置: 首页 > 知识库问答 >
问题:

使用外键添加laravel迁移

昝卓
2023-03-14

我创建了一个名为用户的表。有一些表被称为公司、名称、部门。我想添加company_id,designation_id,department_id列到用户表作为外键。

我试过了,但没用

public function up()
{
    Schema::table('users', function (Blueprint $table) {

        $table->integer('department_id');
        $table->integer('company_id');
        $table->integer('designation_id');

        $table->foreign('department_id')->references('id')->on('departments')->onDelete('restrict')->onUpdate('restrict');
        $table->foreign('company_id')->references('id')->on('companies')->onDelete('restrict')->onUpdate('restrict');
        $table->foreign('designation_id')->references('id')->on('designations')->onDelete('restrict')->onUpdate('restrict');



    });
}


public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn(['department_id','company_id','designation_id']);
    });
}

当我迁移迁移时,它会显示此错误。

Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表lanwadbusers(错误号:150“外键约束格式不正确”)(SQL:alter tableusers添加约束users\u department\u id\u Foreign外键(department\u id)参考部门id)删除限制更新限制)

名称如下:,

public function up()
    {
        Schema::create('designations', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestampsTz();
        });
    }

部门迁移如下,

public function up()
    {
        Schema::create('departments', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('company_id');
            $table->timestampsTz();
        });
    }
    ```





共有2个答案

丁良骏
2023-03-14

使用以下命令:

    $table->integer('department_id')->unsigned()->nullable();
    $table->integer('company_id')->unsigned()->nullable();
    $table->integer('designation_id')->unsigned()->nullable();
    $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade')->onUpdate('cascade');
    $table->foreign('company_id')->references('id')->on('departments')->onDelete('cascade')->onUpdate('cascade');
    $table->foreign('designation_id')->references('id')->on('departments')->onDelete('cascade')->onUpdate('cascade');
唐昊焜
2023-03-14

要建立关系,外键字段应为索引。这里有三列要用作外键'company\u id''department\u id''designation\u id'。在Laravel迁移中,可以使用unsigned()函数对它们进行索引。

例子:

$table->integer('department_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->integer('designation_id')->unsigned();

还有另一个名为unsignedInteger()的函数,通过该函数可以使列同时为整数和无符号。

$table->unsignedInteger('department_id');
$table->unsignedInteger('company_id');
$table->unsignedInteger('designation_id');

 类似资料:
  • 我花了一整天的时间试图弄清楚下面的迁移发生了什么。 这是表的迁移。我的想法是使用“id_stays_abroad”和“user_id”作为外键。 这是添加外键的另一个迁移 当我运行php artisan迁移时,我遇到了以下错误: SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table

  • 我正在尝试为表创建迁移。此表有两个表的外键约束:和。 orders表的架构: employees表的架构: 客户端表的架构: 运行迁移时,已成功为clients表创建约束,但由于某些原因,在尝试为员工创建约束时出现错误: [Illumb\Database\QueryException]SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter tableadd cons

  • 我试图在Laravel中创建外键,但是当我使用artisan迁移我的表时,我被抛出以下错误: 模块迁移表: 课程迁移表: 任何关于我做错了什么的想法,我想现在就得到这个,因为我需要创建很多表,例如用户、客户端、项目、任务、状态、优先级、类型、团队。理想情况下,我想创建包含外键、i... eclients_project和project_tasks等数据的表。 希望有人能帮助我开始。

  • 我有多个迁移,但我认为与这个问题相关的两个迁移是“作业”和“会话”迁移。 作业迁移

  • 我已经发现了几十个类似的问题,但没有一个解决方案在我的案例中有效。当我运行“php artisan migrate:fresh”时,它抛出了错误。。。 illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter tableadd constraint外键()引用()关于删除级联) > 我检查auth用户表,它

  • 我想在用户表中添加一个外键: 一个用户有一个城镇(法语为ville),一个城镇可以有多个用户: 我的村庄(城镇)迁移 我重命名了迁移(我不知道这样做是否有好处?),这是我迁移的顺序,正如您所看到的,我在用户之前创建了villes: 我在终端中发现了这个错误: 照亮\数据库\查询异常 SQLSTATE[42000]:语法错误或访问冲突:1072键列“villes\u id”在表中不存在(SQL:al