Laravel迁移失败

时间:2015-11-15 20:55:34

标签: mysql sql laravel laravel-migrations laravel-schema-builder

我在使用Laravel Migrations方面遇到了一些麻烦。我最近停止了对C9的开发并决定尝试使用Nitrous.io - 但我不确定我的错误是否与此相关。我还决定从sqlite更改为mysql。因为我有点像SQL Noob;我很难搞清楚这一点。

我收到此错误:

[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 not null, `tag_id` varchar(255) unsigned
not null, `created_at` timesta' at line 1 (SQL: create table `yoyo_tag` (`id` int unsigned not null auto_increment primary key, `yoyo_id` varchar(255) unsigned not null, `tag_id` varchar(255) unsigned not null, `created_at` timestamp defau
lt 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)

还有这个:

[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 not null, `tag_id` varchar(255) unsigned
not null, `created_at` timesta' at line 1

我的迁移如下:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateYoyoTagsTable extends Migration {

    /**
      * Run the migrations.
      *
      * @return void
      */
    public function up()
    {
        Schema::create('yoyo_tag', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('yoyo_id')->unsigned()->index();
            $table->foreign('yoyo_id')->references('id')->on('yoyo')->onDelete('cascade');

            $table->string('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('yoyo_tag');
    }

}

create table语句如下:

create table `yoyo_tag` (`id` int unsigned not null auto_increment primary key, `yoyo_id` varchar(255) unsigned not null, `tag_id` varchar(255) unsigned not null, `created_at` timestamp default 0 not null, `updated_at` times
tamp default 0 not null) default character set utf8 collate utf8_unicode_ci

1 个答案:

答案 0 :(得分:2)

查看您的迁移,yoyo_idtag_id应该是无符号整数,而不是无符号字符串。在down方法中,您还应该在删除表之前删除外键,因此有效的迁移应如下所示:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateYoyoTagsTable extends Migration {

    /**
      * Run the migrations.
      *
      * @return void
      */
    public function up()
    {
        Schema::create('yoyo_tag', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('yoyo_id')->unsigned()->index();
            $table->foreign('yoyo_id')->references('id')->on('yoyo')->onDelete('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::table('yoyo_tag', function ($table) {
            $table->dropForeign('yoyo_tag_yoyo_id_foreign');
            $table->dropForeign('yoyo_tag_tag_id_foreign');
        });


        Schema::drop('yoyo_tag');
    }

}