简体   繁体   English

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

[英]Laravel Create Eloquent Instance from ManyToOne Relationship Error SQLSTATE[23000]

I want to create instance using Laravel 5 Eloquent Relationship. 我想使用Laravel 5 Eloquent Relationship创建实例。

I have 2 Migrations and 2 Eloquent Model. 我有2个迁移和2个雄辩的模型。

Companies Migration: 公司迁移:

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');
    }

}

Company Model: 公司型号:

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: OrganizationUnits迁移:

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 Model: 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');
    }

}

The relationship is one Company may have one or more OrganizationUnit, one OrganizationUnit must have one and only one Company. 关系是:一个公司可能拥有一个或多个OrganizationUnit,一个OrganizationUnit必须拥有一个且只有一个Company。

I tried to create new instance of OrganizationUnit in php artisan tinker using this code: 我试图使用以下代码在php artisan tinker创建OrganizationUnit的新实例:

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

But Laravel gives the following error: 但是Laravel给出以下错误:

Illuminate\\Database\\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'CompanyCode' cannot be null (SQL: insert into `OrganizationUnits` (`Org anizationUnitCode`, `CompanyCode`, `updated_at`, `created_at`) values (abcdef, , 2015-12-17 00:17:33, 2015-12-17 00:17:33))' 带有消息'SQLSTATE [23000]的Illuminate \\ Database \\ QueryException:违反完整性约束:1048列'CompanyCode'不能为空(SQL:插入到'OrganizationUnits`(“ Org anizationUnitCode`,`CompanyCode`,`updated_at`,`created_at` )值(abcdef,,2015-12-17 00:17:33,2015-12-17 00:17:33))'

Where did I go wrong? 我哪里做错了? Please help. 请帮忙。 I'm new to Laravel. 我是Laravel的新手。

It clearly says that CompanyCode cannot be null. 明确指出CompanyCode不能为null。 You can define it by hand or you can use the increments method on the Blueprint instance while creating your migration. 您可以手动定义它,也可以在创建迁移时在Blueprint实例上使用increments方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel播种错误SQLSTATE [23000] - Laravel Seeding error SQLSTATE[23000] laravel上的mysql SQLSTATE [23000]错误 - mysql SQLSTATE[23000] error on laravel Laravel:PHP Artisan Tinker'SQLSTATE [23000]'错误 - Laravel: PHP Artisan Tinker 'SQLSTATE[23000]' error 提交表单后,Laravel 5.2一对一关系给出错误消息“ SQLSTATE [23000]:” - Laravel 5.2 one to one relationship gives an error mesg “SQLSTATE[23000]:” after form submission 试图在 Laravel 上创建假帖子,但遇到带有消息“SQLSTATE [23000]”的 QueryException - Trying to Create Fake Posts on Laravel but meeting with QueryException with message 'SQLSTATE[23000] 如何解决Laravel和MySql错误,称为SQLSTATE [23000] - How To Solve Laravel and MySql Error Called SQLSTATE[23000] Laravel 5.1 SQLSTATE [23000]:违反完整性约束错误 - Laravel 5.1 SQLSTATE[23000]: Integrity constraint violation error SQLSTATE [23000]:完整性约束违规:1052. Laravel eloquent 连接表时出现问题 - SQLSTATE[23000]: Integrity constraint violation: 1052. Laravel eloquent issue when joining table Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous Laravel: eloquent 关系创建多 - Laravel: eloquent relationship create multi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM