【问题标题】:Laravel 5 foreign key migration errorLaravel 5外键迁移错误
【发布时间】:2015-08-14 08:48:28
【问题描述】:

我在尝试重置迁移时遇到问题。我正在使用 oracle sql,所以当问题出现时,我首先认为我已经完成了,我现在在我的 sqldeveloper 中清空了数据库,我看不到表,但是如果我运行 tinker,每个表都会被创建。当我试图回滚我的迁移时,我收到一个错误,指出我在某些表中有外键:外键引用的表中的唯一/主键,但是当我尝试删除约束时,工匠说该表没有有这个约束。

这是我的迁移文件: 类别表:

public function up()
{
    Schema::create('categories', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->integer('parent');
    });
    Schema::create('article_category', function(Blueprint $table){
        $table->integer('article_id')->unsigned()->index();
        $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
        $table->integer('category_id')->unsigned()->index();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });
}

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

}

文章表:

public function up()
{
    Schema::create('articles', function(Blueprint $table){

        $table->increments('id');
        $table->longText('title');
        $table->integer('user_id')->unsigned();
        $table->string('excerpt',255);
        $table->longText('body');
        $table->integer('likes')->default(0);
        $table->longText('sourceName');
        $table->longText('linkURL');
        $table->integer('views')->default(0);
        $table->timestamp('published_at');
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

    });


}



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

我的角色表:

public function up()
{
    Schema::create('roles', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
    Schema::create('role_user', function(Blueprint $table){
        $table->integer('role_id')->unsigned()->index();
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });
}

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

我的标签表:

public function up()
{
    Schema::create('tags', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
    Schema::create('article_tag', function(Blueprint $table){
        $table->integer('article_id')->unsigned()->index();
        $table->foreign('article_id')->references('id')->on('articles')->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::drop('article_tag');
    Schema::drop('tags');
}

所以,基本上我的数据库是空的,但我无法运行任何东西。如果我运行 php artisan migrate 不会创建任何表,但是如果我运行 php artisan migrate:refresh/rollback/reset unique/primary keys in table by foreign key referenced on drop table article。 我已经搜索过这个,但我没有找到任何适合我的问题的东西希望我能在这里找到解决方案

【问题讨论】:

    标签: php oracle laravel migration laravel-5


    【解决方案1】:

    您应该在删除表之前dropForeign

    例子:

    public function up()
    {
        Schema::create('tags', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
        Schema::create('article_tag', function(Blueprint $table){
            $table->integer('article_id')->unsigned()->index();
            $table->foreign('article_id')->references('id')->on('articles')->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('article_tag', function (Blueprint $table) {
            $table->dropForeign('article_tag_article_id_foreign');
            $table->dropForeign('article_tag_tag_id_foreign');
        });
        Schema::drop('article_tag');
        Schema::drop('tags');
    }
    

    【讨论】:

    • 首先我认为这是我的问题,因为我没有正确编写 dropforeign 语句,但后来我按照你说的尝试了,它给了我:不能删除约束 - 外键不存在约束。我在我的迁移文件中添加了这个: Schema::table('articles', function (Blueprint $table) { $table->dropForeign('articles_user_id_foreign'); });
    • 我清空了我的数据库,这可能是个问题吗?
    • 我不知道,但要确保您可以删除数据库并重新开始。
    猜你喜欢
    • 2015-04-18
    • 2019-01-31
    • 2014-09-27
    • 2018-09-22
    • 2016-07-06
    • 2015-10-18
    • 1970-01-01
    • 2015-03-02
    • 2019-02-09
    相关资源
    最近更新 更多