php – Laravel Database Schema,Nullable Foreign
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Laravel Database Schema,Nullable Foreign,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2500字,纯文字阅读大概需要4分钟。
内容图文
我有这两个数据库表:
>用户表
>合作伙伴表
用户表将处理此类信息
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))
我知道这是由伙伴表所需的countries表引起的.
我的问题是:是否有一个解决方案,所以我可以填写合作伙伴表上的国家或任何其他非必需数据,但保留国家,州等的外国表架构.
解决方法:
像这样设置country_id和state_id可以为空.
$table->integer('country_id')->nullable()->unsigned();
$table->integer('state_id')->nullable()->unsigned();
内容总结
以上是互联网集市为您收集整理的php – Laravel Database Schema,Nullable Foreign全部内容,希望文章能够帮你解决php – Laravel Database Schema,Nullable Foreign所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。