Laravel从ManyToOne关系错误SQLSTATE创建雄辩实例[23000]

时间:2015-12-16 17:22:16

标签: php mysql laravel laravel-5 eloquent

我想使用Laravel 5 Eloquent Relationship创建实例。

我有2个迁移和2个雄辩模型。

公司迁移:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCompaniesTable extends Migration {

    public function up()
    {
        Schema::create('Companies', function(Blueprint $table)
        {
            $table->string('CompanyCode', 15);
            $table->string('Name', 200);
            $table->string('Type', 100);
            $table->tinyInteger('IsActive')->default(1);
            $table->timestamps();

            $table->primary('CompanyCode');
        });
    }

    public function down()
    {
        Schema::drop('Companies');
    }

}

公司型号:

namespace App\Models\Setting\Organization;

use Illuminate\Database\Eloquent\Model;

class Company extends Model {

    protected $fillable = ['CompanyCode', 'Name', 'Type'];

    public function organizationUnits(){
        return $this->hasMany('App\Models\Setting\Organization\OrganizationUnit', 'CompanyCode');
    }
}

OrganizationUnits Migration:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrganizationUnitsTable extends Migration {

    public function up()
    {
        Schema::create('OrganizationUnits', function(Blueprint $table)
        {
            $table->string('OrganizationUnitCode', 15); //PK
            $table->string('CompanyCode', 15); //FK
            $table->string('Name', 200);
            $table->tinyInteger('IsActive')->default(1);
            $table->timestamps();

            $table->primary('OrganizationUnitCode');
            $table->foreign('CompanyCode', 'OrgUnits_Company_FK')
                  ->references('CompanyCode')
                  ->on('Companies')
                  ->onDelete('cascade');
        });

    }

    public function down()
    {
        Schema::drop('OrganizationUnits');
    }

}

OrganizationUnit模型:

namespace App\Models\Setting\Organization;

use Illuminate\Database\Eloquent\Model;

class OrganizationUnit extends Model {

    protected $table = "OrganizationUnits";

    protected $fillable = ['OrganizationUnitCode', 'CompanyCode', 'Name'];

    public function company(){
        return $this->belongsTo('App\Models\Setting\Organization\Company', 'CompanyCode');
    }

}

关系是一个公司可能有一个或多个OrganizationUnit,一个OrganizationUnit必须有一个且只有一个公司。

我尝试使用以下代码在php artisan tinker中创建OrganizationUnit的新实例:

$company = \App\Models\Setting\Organization\Company::first();
$orgunit = $company->organizationUnits()->create(['OrganizationUnitCode' => 'abcdef']);

但是Laravel给出了以下错误:

  

Illuminate \ Database \ QueryException,消息'SQLSTATE [23000]:完整性约束违规:1048列'CompanyCode'不能为空(SQL:插入`OrganizationUnits`(`Org   anizationUnitCode`,`CompanyCode`,`updated_at`,`created_at`)值(abcdef ,, 2015-12-17 00:17:33,2015-12-17 00:17:33))'

我哪里出错了?请帮忙。我是Laravel的新手。

1 个答案:

答案 0 :(得分:1)

它清楚地表明CompanyCode不能为空。您可以手动定义它,也可以在创建迁移时使用Blueprint实例上的increments方法。