【问题标题】:Laravel Foreign Key Migration reference to String IssueLaravel 外键迁移参考字符串问题
【发布时间】:2015-03-31 15:27:53
【问题描述】:

我想知道是否有可能在 laravel 迁移中添加字符串引用的外键。所以,我在第一次文件迁移时有这个代码。

public function up()
{
    // roles table
    Schema::create('roles', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('level')->unsigned();
        $table->string('title');
        $table->unique('level');
    });

    // account table
    Schema::create('account', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('acc_no');
        $table->string('acc_username');
        $table->string('acc_password');
        $table->integer('acc_roles_level')->unsigned();
        $table->softDeletes();
        $table->timestamps();
        $table->unique('acc_username');
    });

然后在第二个迁移文件我有这个代码:

public function up()
{
    Schema::table('account', function(Blueprint $table)
    {
        $table->foreign('acc_roles_level')
              ->references('level')->on('roles')
              ->onUpdate('cascade')
              ->onDelete('set null');
    });
}

迁移运行时显示错误:

SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table `account` 添加约束 account_acc_roles_level_foreign 外键(`acc_roles_level`)在删除时引用`roles`(`level`)在更新时设置为空级联)更新级联)

我不知道为什么我不能引用字符串列。同时,我在mysql上成功运行了这段代码,没有错误: alter table account add foreign key acc_roles_level references roles(level)

有没有人遇到过类似的问题?

【问题讨论】:

  • 您不应该在各自的表中同时将acc_roles_levellevel 都设为未签名吗?
  • @NomanUrRehman 是的,当然,我都没有签名。
  • 我认为问题是level 列不是roles 表中的主键,您将其添加为外键。外键不应该是自己表中的主键吗?
  • @NomanUrRehman 不一定。外键可以引用任何唯一的列,即使它不是主键。
  • 我也看到了很多这种实现的例子。但是,它主要只是将外键引用到整数类型。有没有可能我可以将它引用到字符串类型列?

标签: php mysql laravel database-migration


【解决方案1】:

您必须更改迁移顺序

|-database/
|--migrations/
|---2015_02_02_141725_create_account_table.php
|---2015_03_01_234626_create_roles_table.php

应该是:

|-database/
|--migrations/
|---2015_01_01_234626_create_roles_table.php
|---2015_02_02_141725_create_account_table.php

最后:

php artisan migrate

【讨论】:

    【解决方案2】:

    经过大量搜索后,我将表格定义从

    用户

    public function up()
    {
    Schema::create('users', function (Blueprint $table) {
      $table->string('user_id');
      $table->string('short_name');
      $table->string('display_name');
      $table->string('username');
      $table->string('email');
    }
    

    public function up()
    {
    Schema::create('users', function (Blueprint $table) {
      $table->engine = 'InnoDB'; // <- add this
      $table->string('user_id')->primary(); // <- and this
      $table->string('short_name');
      $table->string('display_name');
      $table->string('username');
      $table->string('email');
    }
    

    bonus_user

    public function up()
    {
    Schema::create('bonus_user', function (Blueprint $table) {
      $table->engine = 'InnoDB';
      $table->string('bonus_id');
      $table->string('user_id');
    });
    
    Schema::table('bonus_user', function (Blueprint $table) {
      $table->foreign('bonus_id')->references('bonus_id')->on('bonuses')->onDelete('cascade');
      $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
    });
    

    }

    innoDB我从这里得到SO laravel foreign key migration ref string issue(见答案2)

    primary() 我从这里得到enter link description here (尽管我没有遵循答案的任何其他部分,因为我必须将主键作为字符串提供给我一个 24 字符的字符串!)

    我认为这 2 个更改可以一起使用,因为它不能仅与 InnoDB 引擎更改一起使用。

    迁移顺序也可能是解决方案的一部分,但由于我的bonus_user 表引用了这个外键是在原始用户表迁移之后生成的,所以创建顺序问题对我来说不是问题!

    在我将 primary() 添加到 user_id 列之后它第一次工作!

    【讨论】:

      猜你喜欢
      • 2014-08-09
      • 2014-03-11
      • 1970-01-01
      • 2015-01-13
      • 2013-05-31
      • 2013-08-27
      • 2020-08-01
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多