雖然插入數據到mysql數據庫我得到下面的錯誤,並且一些以前的問題的答案stackoverflow我把protected $fillable
在我的模型文件,但仍然顯示錯誤。當我創建我的第一個條目表的接受沒有任何錯誤,但是當我正在做我的新條目其示值誤差插入數據時的錯誤數據庫的數據庫,
。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`app`.`articles`, CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `users` (`id`)) (SQL: insert into `articles` (`article_head`, `article_body`) values (abkskfbasfbv, zbfvaksdvaskdvjsdc
))
Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'article_head', 'article_body',
];
public $timestamps = false;
}
遷移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateArticlesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function(Blueprint $table)
{
$table->increments('article_id');
$table->string('article_head', 500);
$table->string('article_body', 10000);
$table->dateTime('created_on');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('articles');
}
}
2016_04_27_181606_add_foreign_keys_to_articles_table.php
<?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');
});
}
}
可能重複的[完整性約束違規:1452無法添加或更新子行:](http://stackoverflow.com/questions/14063652/integrity-constraint-violation-1452-cannot-add-or-update-一個子行) – e4c5
所以我需要添加id列到我的文章表相同的用戶表? –