在laravel 5应用程序中创建外键时出错

时间:2016-06-02 10:18:52

标签: php mysql database laravel laravel-5

我正在创建一个Laravel应用程序,但在创建外键时,我遇到了错误。我有2个表用户和文章,我想连接到表,以便每个用户将有单独的文章。

您可以在https://github.com/Jatinbalodhi96/Laravel-Blogger-Application

上查看更多代码
FOREIGN KEY (article_id) REFERENCES Persons(user_id)

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`app`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`id`) REFERENCES `articles` (`article_id`)) (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (jatin, jatinbalodhi@gmail.com, 

迁移:

<?php

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

class AddForeignKeysToArticlesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->foreign('article_id', 'articles_ibfk_1')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
        });
    }


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

}
<?php

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

class AddForeignKeysToArticlesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->foreign('article_id', 'articles_ibfk_1')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
        });
    }


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

}

堆栈追踪:

    in Connection.php line 673
at Connection->runQueryCallback('insert into `articles` (`article_head`, `article_body`) values (?, ?)', array('asdsdad', 'asdcdsdsf'), object(Closure)) in Connection.php line 629
at Connection->run('insert into `articles` (`article_head`, `article_body`) values (?, ?)', array('asdsdad', 'asdcdsdsf'), object(Closure)) in Connection.php line 409
at Connection->statement('insert into `articles` (`article_head`, `article_body`) values (?, ?)', array('asdsdad', 'asdcdsdsf')) in Connection.php line 365
at Connection->insert('insert into `articles` (`article_head`, `article_body`) values (?, ?)', array('asdsdad', 'asdcdsdsf')) in Processor.php line 32
at Processor->processInsertGetId(object(Builder), 'insert into `articles` (`article_head`, `article_body`) values (?, ?)', array('asdsdad', 'asdcdsdsf'), 'article_id') in Builder.php line 1963
at Builder->insertGetId(array('article_head' => 'asdsdad', 'article_body' => 'asdcdsdsf'), 'article_id')
at call_user_func_array(array(object(Builder), 'insertGetId'), array(array('article_head' => 'asdsdad', 'article_body' => 'asdcdsdsf'), 'article_id')) in Builder.php line 1337
at Builder->__call('insertGetId', array(array('article_head' => 'asdsdad', 'article_body' => 'asdcdsdsf'), 'article_id')) in Model.php line 1621
at Builder->insertGetId(array('article_head' => 'asdsdad', 'article_body' => 'asdcdsdsf'), 'article_id') in Model.php line 1621
at Model->insertAndSetId(object(Builder), array('article_head' => 'asdsdad', 'article_body' => 'asdcdsdsf')) in Model.php line 1590
at Model->performInsert(object(Builder), array()) in Model.php line 1481
at Model->save() in ArticleEditor.php line 19
at ArticleEditor->insert_Article(object(Request))
at call_user_func_array(array(object(ArticleEditor), 'insert_Article'), array(object(Request))) in Controller.php line 80
at Controller->callAction('insert_Article', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ArticleEditor), object(Route), 'insert_Article') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ArticleEditor), object(Route), object(Request), 'insert_Article') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ArticleEditor', 'insert_Article') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\Laravel_apps\Blogger\public\index.php') in server.php line 21

2 个答案:

答案 0 :(得分:0)

试试这个,

$table->foreign('article_id')
    ->references('id')
    ->on('users')
    ->onUpdate('RESTRICT')
    ->onDelete('RESTRICT');

您可以使用以下命令在迁移中启用或禁用外键约束:

Schema::enableForeignKeyConstraints();
Schema::disableForeignKeyConstraints();

答案 1 :(得分:0)

你的问题是你试图插入没有正确的关系。 Mysql会阻止你这样做(导致你在上次迁移中定义关系)

您应该在文章中添加ID。

$article = new Article();
$article->id = $userId;  // make sure this id is exist in users table;
// other column you need
$article->save();

参考:https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models