Ricardo Cruz Ricardo Cruz - 6 months ago 12
SQL Question

I cant do a foreing 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 ma nual that corresponds to your MySQL server version for the
right syntax to use near 'unsigned null' at lin e 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 ma nual that
corresponds to your MySQL server version for the right syntax to use
near 'unsigned null' at lin e 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 tring to do is to add to the
files table
the
*slug column*
from the
slugs table
.

Answer

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