duanliao2310 2014-09-04 18:06 采纳率: 100%
浏览 126
已采纳

laravel上的mysql SQLSTATE [23000]错误

I am using laravel schema builder and here is my code...

    //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

  • 写回答

1条回答 默认 最新

  • duandao2083 2014-09-04 18:13
    关注

    If the SQL statement's to be believed somewhere the app's inserting just the timestamps fields (updated_at, created_at). You need to at least specify an existing user_id for the query. 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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法