Eloquent不通过关联插入不是整数

时间:2016-04-08 16:29:38

标签: php laravel eloquent

我为lang创建了以下迁移:

class CreateLangTable extends Migration
{

public function up()
{
    Schema::create('lang', function (Blueprint $table) {
        $table->string('code')->primary();
        $table->string('name');
        $table->timestamps();
    });
}


}
class CreateMachineryCategoriesTranslationTable extends Migration
{

public function up()
{
    Schema::create('machinery_categories_translation', function (Blueprint $table) {
        $table->increments('id');
        $table->string('lang_code')->index();
        $table->foreign('lang_code')->references('code')->on('lang')->onDelete('cascade')->onUpdate('cascade');
        $table->integer('machinery_categories_id')->unsigned()->index();
        $table->foreign('machinery_categories_id')->references('id')->on('machinery_categories')->onDelete('cascade')->onUpdate('cascade');
        $table->string('name')->nullable();
        $table->timestamps();
    });
}

但是当我尝试播种时:

class MachineryCategoriesTranslationTableSeeder extends Seeder
{

public function run()
{
    $el = Lang::find('el');
    $en = Lang::find('en');

    $loaderCat = new MachineryCategory();
    $loaderCat->save();

    $loaderCatEl = new MachineryCategoriesTranslation();
    $loaderCatEl->machineryCategories()->associate($loaderCat);
    $loaderCatEl->lang()->associate($el);
    $loaderCatEl->name = 'Φορτωτές';
    $loaderCatEl->save();

    $loaderCatEn = new MachineryCategoriesTranslation();
    $loaderCatEn->machineryCategories()->associate($loaderCat);
    $loaderCatEn->lang()->associate($en);
    $loaderCatEn->name = 'Loaders';
    $loaderCatEn->save();
}

}

我收到错误

 SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or    update a child row: a foreign key constraint fails (`ps4cat`.`machinery_categories_translation`, CONSTRAINT `machinery_categories_translation_lang_code_foreign` FOREIGN KEY (`lang_code`) REFERENCES `lang` (`code`) ON DELETE CASCADE ON UPDATE CA) 

(SQL: insert into `machinery_categories_translation` (`machinery_categories_id`, `lang_code`, `name` , `updated_at`, `created_at`) values (1, 0, Φορτωτές, 2016-04-08 16:24:06, 2016-04-08 16:24:06))  

0 values中的lang_code显然是错误的。有什么问题?

0 个答案:

没有答案
相关问题