【发布时间】:2015-06-18 03:38:54
【问题描述】:
我正在使用 Laravel 5 向数据库中插入一些条目,但出现以下错误:
SQLSTATE[23000]:完整性约束违规:1062 键“category_id”的重复条目“1”(SQL:插入
products(product_name,product_price,category_id)值(asdaszzz,123, 1))
据我了解,问题是我想在 category_id 表中添加的值已经存在于其他条目中,但问题是 category_id 表不是唯一键。下面我发布了迁移:
public function up()
{
Schema::create('products', function($table)
{
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('product_name', 255)->unique();
$table->decimal('product_price', 10, 4);
$table->dateTime('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'));
});
Schema::table('products', function($table) {
$table->foreign('category_id')->references('id')->on('categories');
});
}
【问题讨论】: