简体   繁体   中英

I can't do a foreign key, constraint error

I'm having a commum error and I can't go over it, know as [Illuminate\\Database\\QueryException] SQLSTATE[42000] here is the full error:

[Illuminate\\Database\\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unsigned null' at line 1 (SQL: alter table files add slug varchar(255) unsigned null)

A separated error:

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unsigned null' at line 1

He are the tables I'm trying to do the foreign key:

Files

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

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

What I'm trying to do is to add to the files table the *slug column* from the slugs table .

I guess string data type cannot be unsigned() , that's why you're getting an error.

Use this in both migrations:

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

As I'm using OctoberCMS with Laravel, there are relationships like $hasMany and $belongsTo on the **models** file .

If are in trouble with this on OctoberCMS, just consult this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM