Laravel 迁移失败

Posted

技术标签:

【中文标题】Laravel 迁移失败【英文标题】:Laravel Migration Failed 【发布时间】:2016-02-16 22:27:23 【问题描述】:

我在使用 Laravel 迁移时遇到了一些麻烦。我最近停止在 C9 上进行开发并决定尝试使用 Nitrous.io - 但是我不确定我的错误是否与此有关。我还决定从 sqlite 更改为 mysql。因为我有点 SQL 菜鸟;我很难弄清楚这一点。

我收到此错误:

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

【问题讨论】:

我按要求在帖子中添加了create table语句。 无符号字符串?这是新事物:) 哦,伙计;我是个白痴!谢谢! 【参考方案1】:

查看您的迁移,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');
    


【讨论】:

感谢您的帮助;不幸的是,随后又出现了另一个错误。这次关联外键。有任何想法吗? [Illuminate\Database\QueryException] SQLSTATE[HY000]: 一般错误: 1005 Can't create table 'homestead.#sql-2d37_4e' (errno: 150) (SQL: alter table yoyo_tag add constraint yoyo_tag_yoyo_id_foreign 外键 (@987654326) @) 在删除级联时引用 yoyo (id) 忽略我的评论我终于更正了,一切都很好!我错误地将表命名为“yoyo”,它是“yoyos”……真是个愚蠢的错误。

以上是关于Laravel 迁移失败的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 工匠迁移失败

Laravel 迁移,在 hasColumn 方法上失败

Php工匠迁移失败Laravel [重复]

Laravel 5.4 迁移 ENUM 在 MySQL 中失败

Laravel迁移,hasColumn方法失败

Laravel 单元测试迁移失败