第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Laravel 7,SQLSTATE [23000]:完整性約束違規(guī):

Laravel 7,SQLSTATE [23000]:完整性約束違規(guī):

PHP
千萬里不及你 2022-10-28 09:39:21
我正在使用 MySQL 8.0 在 PHP 7.4 上運(yùn)行 Laravel 7。我有三個(gè)表,User和Company,Department以及它們各自的模型和工廠。我創(chuàng)建了一個(gè)要添加關(guān)系的測試:// MyTest.php$user = factory(User::class)->create();$company = factory(Company::class)->make();$company->user()->associate($user);$company->create(); // it fails here because of NOT NULL constraint, companies.user_id$department = factory(Department::class)->make();$department->company()->associate($company);$department->create();我收到以下錯(cuò)誤:Integrity constraint violation: 19 NOT NULL constraint failed: companies.user_id (SQL: insert into "companies" ("updated_at", "created_at") values (2020-03-10 07:27:51, 2020-03-10 07:27:51))我的表模式定義如下:// usersSchema::create('users', function (Blueprint $table) {    $table->id();    $table->string('name');    $table->string('email')->unique();    $table->timestamp('email_verified_at')->nullable();    $table->string('phone');    $table->integer('user_type');    $table->string('password');    $table->rememberToken();    $table->timestamps();});// companiesSchema::create('companies', function (Blueprint $table) {    $table->id();    $table->foreignId('user_id')->constrained()->onDelete('cascade');    $table->string('name');    $table->string('contact_email');    $table->string('contact_phone');    $table->timestamps();});// departmentsSchema::create('departments', function (Blueprint $table) {    $table->id();    $table->foreignId('company_id')->constrained()->onDelete('cascade');    $table->string('name');    $table->string('contact_email');    $table->string('contact_phone');    $table->timestamps();});->nullable()我的理解是 SQL 表中不應(yīng)該有 NULL 值,這就是為什么我在遷移中刻意避免使用的原因。特別是對于像這樣的外鍵。
查看完整描述

1 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

乍一看,您的表結(jié)構(gòu)的一些問題非常明顯。

  • 您似乎正在嘗試向表中添加一user_idcompanies。假設(shè)您的公司有不止一名員工,這不是一個(gè)好主意。

  • 如果要使用NOT NULL列,最好為每個(gè)列定義一個(gè)默認(rèn)值。

所以我們可以從編寫類似這樣的遷移開始,包括公司/用戶和部門/用戶關(guān)系的數(shù)據(jù)透視表:

// companies

Schema::create('companies', function (Blueprint $table) {

    $table->id();

    $table->string('name');

    $table->string('contact_email')->default('');

    $table->string('contact_phone')->default('');

    $table->timestamps();

});


// departments

Schema::create('departments', function (Blueprint $table) {

    $table->id();

    $table->foreignId('company_id')->constrained()->onDelete('cascade');

    $table->string('name');

    $table->string('contact_email')->default('');

    $table->string('contact_phone')->default('');

    $table->timestamps();

});


// users

Schema::create('users', function (Blueprint $table) {

    $table->id();

    $table->string('email')->unique();

    $table->timestamp('email_verified_at')->nullable();

    $table->string('name')->default('');

    $table->string('phone')->default('');

    $table->integer('user_type')->default(0);

    $table->string('password');

    $table->rememberToken();

    $table->timestamps();

});


Schema::create('company_user', function (Blueprint $table) {

    $table->id();

    $table->foreignId('user_id')->constrained()->onDelete('cascade');

    $table->foreignId('company_id')->constrained()->onDelete('cascade');

});


Schema::create('department_user', function (Blueprint $table) {

    $table->id();

    $table->foreignId('user_id')->constrained()->onDelete('cascade');

    $table->foreignId('department_id')->constrained()->onDelete('cascade');

});

現(xiàn)在我們有了表之間的鏈接。部門是公司的一部分;一個(gè)用戶可以是多個(gè)部門和/或多個(gè)公司的一部分。這導(dǎo)致以下關(guān)系:


class User extends Model {

    // many-to-many

    public function companies() {

        return $this->belongsToMany(App\Company::class);

    }

    // many-to-many

    public function departments() {

        return $this->belongsToMany(App\Department::class);

    }

}


class Company extends Model {

    public function departments() {

        // one-to-many

        return $this->hasMany(App\Department::class);

    }

    public function users() {

        // many-to-many

        return $this->belongsToMany(App\User::class);

    }

}


class Department extends Model {

    public function company() {

        // one-to-many (inverse)

        return $this->belongsTo(App\Company::class);

    }

    public function users() {

        // many-to-many

        return $this->belongsToMany(App\User::class);

    }

}

現(xiàn)在這樣的代碼應(yīng)該可以工作了:


$user = factory(User::class)->create();

$company = factory(Company::class)->create();


$user->companies()->attach($company);

$company->departments()->create([

    'name' => 'Department 1',

    'contact_email' => 'department1@example.test',

    'contact_phone' => '123456789',

]);

具體來說,該attach方法用于根據(jù)您的原始表格布局更新您似乎沒有定義的多對多關(guān)系。


查看完整回答
反對 回復(fù) 2022-10-28
  • 1 回答
  • 0 關(guān)注
  • 97 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號