Php Laravel数据库架构,可为空的外部
我有两个数据库表:Php Laravel数据库架构,可为空的外部,php,mysql,database,laravel,Php,Mysql,Database,Laravel,我有两个数据库表: 用户表 合作伙伴表 用户表将处理此类信息 Schema::create('users', function (Blueprint $table) { $table->increments('id')->unique(); $table->string('email')->unique(); $table->string('username')->unique(); $table->str
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unique();
$table->string('email')->unique();
$table->string('username')->unique();
$table->string('password', 60);
$table->string('photo')->nullable();
$table->integer('partner_id')->unsigned();
$table->foreign('partner_id')->references('id')->on('partners');
$table->rememberToken();
$table->timestamps();
});
而合作伙伴表将包含所有用户元信息,如名字和姓氏等
Schema::create('partners', function (Blueprint $table) {
/**
* Identity Columns
*/
$table->increments('id')->unique();
$table->string('first_name');
$table->string('middle_name')->nullable();
$table->string('last_name')->nullable();
$table->string('display_name')->nullable();
$table->string('email')->unique()->nullable();
$table->string('website')->nullable();
$table->string('phone')->nullable();
$table->string('mobile')->nullable();
$table->string('fax')->nullable();
$table->date('birthdate')->nullable();
$table->longText('bio')->nullable();
$table->string('lang')->nullable(); //Language
/**
* Address Columns
*/
$table->text('street')->nullable();
$table->text('street2')->nullable();
$table->integer('country_id')->unsigned(); // foreign
$table->foreign('country_id')->references('id')->on('countries');
$table->integer('state_id')->unsigned(); // foreign
$table->foreign('state_id')->references('id')->on('country_states');
$table->string('city')->nullable();
$table->string('district')->nullable();
$table->string('area')->nullable();
$table->string('zip')->nullable();
});
当用户注册到站点时,我只需要几个字段,用户名
,电子邮件地址
,密码
,名字
和姓氏
。这些只是必填字段
因此,在用户完成站点注册后,可以填写合作伙伴表中的信息
但是由于外键的结构,我无法继续,因为这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mytable`.`tbl_partners`, CONSTRAINT `partners_country_id_foreign` FOREIGN KEY (`country_id`) REFERENCES `tbl_countries` (`id`)) (SQL: insert into `tbl_partners` (`first_name`, `last_name`, `display_name`, `email`, `updated_at`, `created_at`) values (Jack, Wilson, admin, admin@example.com, 2016-06-09 19:41:18, 2016-06-09 19:41:18))
我知道这是由合作伙伴表要求的国家表引起的。我的问题是:是否有一个解决办法,这样我可以在partner表中填写国家或任何其他非必需的数据,但保留国家、州等的外部表架构。将
国家id
和国家id
设置为空,就像这样
$table->integer('country_id')->nullable()->unsigned();
$table->integer('state_id')->nullable()->unsigned();
对于最新版本的Laravel,您可以将
null
方法与foreignKey
结合使用:
$table
->foreignId('other_table_id')
->nullable() // here
->references('id')
->on('other_table');
对于Laravel 7.x,我使用以下方法:
$table->bigInteger('word_type_id')->nullable()->unsigned();
$table->index('word_type_id')->nullable();
$table->foreign('word_type_id')->nullable()->references('id')->on('word_types')->onDelete('cascade');
要让laravel 7.x创建一个可为空的外键,只需使用:
$table->foreignId('country_id')->nullable()->constrained();
$table->foreignId('state_id')->nullable()->constrained();
请记住:可空字段应在受约束之前,否则不会影响可空字段。如果在创建架构后需要更改可空字段,则该行应为$table->integer('country_id')->nullable()->unsigned()->change();非常好,我的朋友我如何定义
nullable()->unsigned()
字段的关系?这也是$table->foreign('country_id')->在('countries')
上引用('id')->所需要的吗?确实有特殊的行为。我也很感兴趣为什么nullable()
应该放在第一位。如果父表上的外来id是uuid,我该如何添加?@theodory类似于:$table->foreignUuid('business_id')->nullable()->引用('id')->on('business')->onDelete('cascade')->onUpdate('cascade')代码>