2016-05-13 55 views
4

我有一个commum错误,我不能去了吧,知道作为[Illuminate\Database\QueryException] SQLSTATE[42000]这里是完整的错误:我不能做一个外键约束错误

[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)

分离的错误:

[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

他是我想要做的外键的表:

文件

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

我试图做的是从slugs table添加到files table*slug column*

回答

0

由于我使用OctoberCMS与Laravel,有relationships喜欢上了**models** file$hasMany$belongsTo。  

如果有麻烦与此有关OctoberCMS,只是咨询this

+0

请避免链接唯一的答案,但引用为您的解决方案的相关部分。 – k0pernikus

3

我猜string数据类型不能是unsigned(),这就是为什么你得到一个错误。

两个迁移使用此:

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

Mhhh我将其更改为'$表 - >整数( '塞') - >可空();'我是越来越dispeared错误,但我有另外一个''[Illuminate \ Database \ QueryException] SQLSTATE [HY000]:一般错误:1005无法创建表'october。#sql-3f9_3b'(errno:150)(SQL:alter tab le'files '添加约束文件_slug_foreign forei gn键('slug')引用'slugs'('slug'))'' –

+0

尝试单独迁移。暂时从目录中移除'files'迁移并运行'php artisan migrate'命令。然后移回此迁移并再次运行该命令。 –

+0

仍然是一样的错误。 –