Laravel数据库架构,可为空 [英] Laravel Database Schema, Nullable Foreign
问题描述
我有以下两个数据库表:
I've these two database tables:
- 用户表
- 合作伙伴表
用户表将处理此类信息
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();
});
合作伙伴表将包含所有用户元信息,例如名字和姓氏等.
While Partner Tables will contains all the user meta information such as first name and last name, etc.
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();
});
当用户注册到站点时,我只需要几个字段,分别为username
,email address
,password
,first name
和last name
.这些只是必填字段.
When a user is register to the site I only want few fields which are, username
, email address
, password
, first name
and last name
. These are only the required fields.
因此,在用户完成向网站注册后,以后可以填充合作伙伴表中的信息.
So information in partner tables can be filled later after the user have finish registering to the site.
但是由于外键的结构,由于这个错误,我无法继续进行下去:
But due to the structure of foreign key, I cannot proceed any further because of this error :
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))
我知道这是由合作伙伴表所需的国家/地区表引起的.
我的问题是:是否有解决方法,所以我可以在合作伙伴表上填写国家或任何其他非必需数据,但保留国家,州等的外部表架构.
I know this is cause by the countries table which is required by the partner table.
My question is : is there a work around so I can fill the country or any other non required data on partner table but keep the foreign table schema for countries, states, etc.
推荐答案
设置country_id
和state_id
可为空,就像这样.
Set the country_id
and the state_id
nullable, like so.
$table->integer('country_id')->nullable()->unsigned();
$table->integer('state_id')->nullable()->unsigned();
这篇关于Laravel数据库架构,可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!