【问题标题】:Foreign key is incorrectly formed外键格式不正确
【发布时间】:2018-01-06 00:41:53
【问题描述】:

当我将主键从增量更改为字符串 (user_id) 时,我的迁移出现了错误。我似乎没有看到我哪里出错了。我的迁移文件没问题,直到我从 (->increments 和另一个 ->integer) 更改为 (->string on both)。问题是当它迁移氏族成员表时,它说外键格式不正确

我之前的代码。好像还可以,在cmd中迁移没什么问题

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('user_id');
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('clanmembers', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
        $table->integer('clan_id')->unsigned();
        $table->foreign('clan_id')->references('clan_id')->on('clans')->onDelete('cascade');
        $table->string('bandrole', 50);
        $table->timestamps();
    });
}

我的代码在我将 ->increments 和 ->integer 更改为两者之后 ->string

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->string('user_id');
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('clanmembers', function (Blueprint $table) {
        $table->string('user_id');
        $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
        $table->integer('clan_id')->unsigned();
        $table->foreign('clan_id')->references('clan_id')->on('clans')->onDelete('cascade');
        $table->string('clanrole', 50);
        $table->timestamps();
    });
}

【问题讨论】:

标签: mysql laravel laravel-migrations


【解决方案1】:

表一

 public function up()
    {
        //
       Schema::create('table_1', function (Blueprint $table) {
        $table->string('user_id',100);
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
        $table->primary(['user_id']);
    });
   }

表二

public function up()
    {
        //
       Schema::create('clanmembers', function (Blueprint $table) {
        $table->string('user_id',100);
        $table->foreign('user_id')->references('user_id')->on('table_1')
        ->onDelete('cascade');
        $table->timestamps();
    });
   }

【讨论】:

  • 这行得通。太感谢了!我很感激。抱歉耽搁了
  • 欢迎。我也很抱歉最近提供了正确的答案。
【解决方案2】:

你需要设置字符串长度 $table->string('user_id', 100); 对于 intiger 类型,如果未设置长度,它会自动使用默认值 11。 但是对于字符串,你需要设置它。

【讨论】:

  • 我已经尝试输入长度(完全按照您的建议),但仍然是同样的错误。仍然错误地形成了外键约束
  • class YourModel 扩展模型 { protected $primaryKey = 'user_id';公共 $incrementing = false; }
  • 请将您的列指定为迁移中的主键 $table->primary(array('user_id'));
  • 现在它不会将用户 ID 放入表中。它说 user_id 不存在
猜你喜欢
  • 2017-05-29
  • 2014-11-20
  • 1970-01-01
  • 2021-11-07
  • 2018-09-04
  • 2017-10-03
  • 2019-07-25
  • 1970-01-01
  • 2017-10-04
相关资源
最近更新 更多