简体   繁体   English

laravel上的mysql SQLSTATE [23000]错误

[英]mysql SQLSTATE[23000] error on laravel

I am using laravel schema builder and here is my code... 我正在使用laravel模式生成器,这是我的代码...

    //create links table
    Schema::create('links', function($table){
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('domain_id');
        $table->string('url');
        $table->softDeletes();
        $table->timestamps();
    });
    Schema::table('links', function($table) {
        $table->index('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $table->index('domain_id');
        $table->foreign('domain_id')->references('id')->on('domains')->onDelete('cascade')->onUpdate('cascade');
    });
}

But i try to save something to this table it throws following error... 但我尝试将某些内容保存到此表中,它引发以下错误...

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`we`.`links`, CONSTRAINT `links_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into `links` (`updated_at`, `created_at`) values (2014-09-04 17:35:53, 2014-09-04 17:35:53)) 

Please suggest if you need anything more to understand this better, help me to fix this. 请提出建议,如果您还需要其他任何信息以更好地理解这一点,请帮助我解决此问题。

thanks 谢谢

If the SQL statement's to be believed somewhere the app's inserting just the timestamps fields (updated_at, created_at). 如果可以相信该SQL语句是在应用程序的位置插入时间戳字段(updated_at,created_at)。 You need to at least specify an existing user_id for the query. 您至少需要为查询指定一个现有的user_id。 If there are cases where you can insert into links without associating a user, you need to update the links table so links.user_id allows for null values. 如果在某些情况下可以在不关联用户的情况下插入links ,则需要更新链接表,以便links.user_id允许使用空值。 You can't update a column on an existing table to accept nulls through the schema manager, you'll need to do that through a manual query. 您无法通过架构管理器将现有表上的列更新为接受空值,而需要通过手动查询来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM