foreign-key-relationship

Entity framework code-first null foreign key

人盡茶涼 提交于 2019-12-27 17:10:06
问题 I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public class User{ public int CountryId { get; set; } public Country Country { get; set; } } public class Country{ public List<User> Users {get; set;} public int CountryId {get; set;} } Error: A foreign key value cannot be inserted because a corresponding

Laravel - Multiple foreign keys and getting id

拜拜、爱过 提交于 2019-12-25 08:27:17
问题 I tried a bunch of different things first but don't seem to be getting a result. Controller: public function all() { $projects = Project::all(); foreach($projects as $project) { $pid = $project->id; } $am = DB::table('projects') ->join('employees', 'projects.am_id', '=', 'employees.id') ->where('projects.id', '=', $pid) ->select('projects.id', 'projects.am_id', 'employees.name') ->first(); $pm = DB::table('projects') ->join('employees', 'projects.pm_id', '=', 'employees.id') ->where('projects

mysql errno: 150 “Foreign key constraint is incorrectly formed”- MariaDB

守給你的承諾、 提交于 2019-12-25 08:06:04
问题 MESSAGE_MAP TABLE CREATE TABLE `message_map` ( `message_from` varchar(15) NOT NULL, `message_id` varchar(15) NOT NULL, `message_to` varchar(15) NOT NULL, `message_status` bit(1) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `message_map` ADD PRIMARY KEY (`message_from`,`message_id`,`message_to`), ADD KEY `FK_ij6tystusydqijqp8lgoigo1c` (`message_id`), USER TABLE CREATE TABLE `user` ( `USER_ID` varchar(15) NOT NULL, `PASSWORD` varchar(15) DEFAULT NULL, `PHONE_NUMBER` varchar

show related table columns in all select of model laravel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 07:59:00
问题 I want show related table columns ( customers.name ) in all select of model (User) laravel. I use accessor laravel. user table: id name customer_id 1 hassan 1 customer table: id name 1 customer1 now use $user = Auth::user(); return $user; I want show: id: 1, name: "hassan", customer_id: { id: 1, name: "customer1" } but show this error: Failed calling App\User::jsonSerialize() class User extends Authenticatable { use EntrustUserTrait; /** * Get the user's customer name. * * @param string

How to add foreign key to MySQL table?

北城以北 提交于 2019-12-25 07:29:37
问题 I use MySQL with InnoDB engine. I double-checked type of columns. But always have: Error Code: 1215. Cannot add foreign key constraint I tried: ALTER TABLE `mail`.`boxes` ADD CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES `mail`.`users` (id) ON UPDATE NO ACTION ON DELETE NO ACTION; and ALTER TABLE `mail`.`boxes` ADD FOREIGN KEY (id) REFERENCES `mail`.`users` (id) Nothing works((( Please, help, what I am doing wrong (except choosing MySQL :-) )? 回答1: If table contains data then you are not able

Multiple foreign keys from the same table

爷,独闯天下 提交于 2019-12-25 05:53:30
问题 i'm building a DB of a graduation projects management system. Students are divided into groups .There is groups table and faculty table. Each group has an advisor and two examiners. i'm confused here. Should i create 3 FKs from the the faculty table? 2 for examiners and 1 for advisor? here is the SQL code: create table groups ( groupID NUMBER not null, nbStudents NUMBER not null, avgGPA DOUBLE NOT NULL, projectName varchar(50) not null, advisorID NUMBER examiner1ID NUMBER examiner2ID NUMBER

How to find field through foreign key in cakephp3.x.x?

雨燕双飞 提交于 2019-12-25 04:42:43
问题 how to try like this, i already write in ItemsTable.php like this public function initialize(array $config) { $this->belongsTo('Categories', [ 'className' => 'Item', 'foreignKey' => 'cid', 'propertyName' => 'name' ]); } How do I display category name instead of Cid ? 来源: https://stackoverflow.com/questions/27718675/how-to-find-field-through-foreign-key-in-cakephp3-x-x

Issue with adding a one-to-one foreign-key connection to a RestKit mapping

大憨熊 提交于 2019-12-25 01:36:14
问题 I am trying to add a connection to a RestKit mapping but without success. My JSON is as follows: { parent: { token: "token" title: "title", description: "description", child_id: 1 } } My CoreData scheme defines that the Parent ManagedObject has a Child relation under the child key (and the opposite). The Parent mapping happens after I already have a corresponding Child object in CoreData, so that's not a problem. I followed some links and discussions around the web in order to solve this, but

Model for child table is always null in the partial view

痞子三分冷 提交于 2019-12-24 19:18:56
问题 In my ASP.NET Core 1.1 , EF-Core 1.1 app , I'm displaying data in a parent view that has partial view . Scenario is similar to the following: one-to-many relationship between Customers and Orders table . User selects a customer from a dropdown in the Parent View that has a partial view that displays corresponding orders for the selected customer. But, I'm always getting model passed to the Partial view always null. Question : What I may be missing here? Note : By placing breakpoints, I've

ActiveRecord relation using an array of foreign keys

扶醉桌前 提交于 2019-12-24 17:02:39
问题 Is that possible to establish a relationship between Tree and Branch such as: class Tree < ActiveRecord::Base has_many :branches end class Branch < ActiveRecord::Base belongs_to :tree end But with an array of foreign keys branch_ids stored in Tree? I know it's the opposite of the default process, but I want to do so (just for testing). Many thanks for any help. 回答1: As Lichtamberg mentioned it is a bad schema. Since you said "just for testing", if branch ids will be a column with comma