我不能做外键,约束错误

时间:2016-05-13 10:51:49

标签: php mysql database laravel octobercms

我有一个通讯错误,我无法重复,知道[Illuminate\Database\QueryException] SQLSTATE[42000]这里是完整的错误:

  

[Illuminate \ Database \ QueryException] SQLSTATE [42000]:语法错误   或访问冲突:1064您的SQL语法有错误;校验   与您的MySQL服务器版本对应的手册   在第1行使用'unsigned null'附近的正确语法(SQL:alter   表files添加slug varchar(255)   unsigned null)

分离错误:

  

[PDOException] SQLSTATE [42000]:语法错误或访问冲突:   1064您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在第1行附近'unsigned null'

他是我正在尝试做外键的表:

文件

  public function up()
    {
        Schema::create('files', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->string('name')->nullable();
            $table->boolean('enable_sch')->nullable();
            $table->datetime('schdate')->nullable();
            $table->string('flsize')->nullable();
            $table->timestamps();
        });
        Schema::table('files', function($table)
        {
          $table->string('slug')->unsigned()->nullable();
          $table->foreign('slug')->references('slug')->on('slugs');
        });
    }

蛞蝓

public function up()
    {
        Schema::create('slugs', function($table)
        {
            $table->engine = 'InnoDB';
            $table->string('nameslug');
            $table->string('slug')->unsigned()->nullable();
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('slugs');
    }

我要做的是从files table添加*slug column* slugs table

2 个答案:

答案 0 :(得分:3)

我猜string数据类型不能是unsigned(),这就是您收到错误的原因。

在两次迁移中都使用它:

$table->string('slug')->nullable();

答案 1 :(得分:0)

由于我正在使用OctoberCMS和Laravel,relationships上有$hasMany$belongsTo **models** file

如果在OctoberCMS上遇到问题,请咨询this