foreign-keys

How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?

这一生的挚爱 提交于 2019-11-30 07:25:39
问题 2019 Update / TLDR; switch to Entity Framework Core (or whatever else) While missing some "Features", EF Core properly honors Alternate Keys (aka Unique Constraints) in addition to Primary Keys and thus does a much better job of honoring Relational Algebra. YMMV otherwise; at least it supports many more SQL schemes correctly. This support added was in the (very outdated) EF Core 1.0 release.. a bit disappointing that the original EF never had this design(ed!) flaw addressed. This may be

mysql Multiple Foreign Keys in a Table to the Same Primary Key

亡梦爱人 提交于 2019-11-30 07:05:26
I have a table user with userID as the primary key. I have another table called Friends . In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user . I want to enforce data integrity. Could I use something like this? ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`) REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE; I want to know is REFERENCES users ( userId , userId ) referencing a column multiple times correctly? The reason I am not creating 2 separate

In django, how to limit choices of a foreignfield based on another field in the same model?

戏子无情 提交于 2019-11-30 06:59:47
I have these models (I have limited the number of fields to just those needed) class unit(models.Model): name = models.CharField(max_length=200) class project(models.Model): name = models.CharField(max_length=200) class location(address): project = models.ForeignKey(project) class project_unit(models.Model): project = models.ForeignKey(project) unit = models.ForeignKey(unit) class location_unit(models.Model): project = models.ForeignKey(project) #Limit the selection of locations based on which project has been selected location = models.ForeignKey(location) #The same here for unit. But I have

SQL design around lack of cross-database foreign key references

不想你离开。 提交于 2019-11-30 06:57:20
问题 For better or worse, we have a solution that relies on multiple databases that all reference a common administration database. Databases ship as part of modules, and not all modules are required for an installation (probably why we have multiple databases in the first place). The admin database is required, however ... so it will always be there. I would like to bring some referential integrity and order to the chaos, but am stymied by SQL server's inability to do cross-database foreign keys.

How to design a database schema to support tagging with categories?

ぐ巨炮叔叔 提交于 2019-11-30 06:12:38
问题 I am trying to so something like Database Design for Tagging, except each of my tags are grouped into categories. For example, let's say I have a database about vehicles. Let's say we actually don't know very much about vehicles, so we can't specify the columns all vehicles will have. Therefore we shall "tag" vehicles with information. 1. manufacture: Mercedes model: SLK32 AMG convertible: hardtop 2. manufacture: Ford model: GT90 production phase: prototype 3. manufacture: Mazda model: MX-5

How to add Foreign Key (MySQL)

纵饮孤独 提交于 2019-11-30 05:43:43
I'm extremely new to SQL and I'm trying to perform a simple ALTER TABLE to create a new column and use it as a foreign key to reference another simple table in my database. I've altered both tables to be InnoDB However, when executing the ALTER TABLE code, I get the following error: Error 1452 Cannot add or update a child row: a foreign key constraint fails (`toys`.<result 2 when explaining filename '#sql-6d4_6'>, CONSTRAINT `#sql-6d4_6_ibfk_1` FOREIGN KEY (`toy_id`) REFERENCES `toys` (`toy_id`)) Below are the DESC of both tables: Table 1: FIELD TYPE NULL KEY EXTRA toy_id int(11) NO PRI auto

Exact Meaning of MySQL's Foreign Key 'on delete restrict' Clause

假如想象 提交于 2019-11-30 05:35:05
I have two MySQL tables: collections and privacy_level . I define them with a foreign key relationship as such: CREATE TABLE collections ( coll_id smallint NOT NULL AUTO_INCREMENT UNSIGNED, name varchar(30) NOT NULL, privacy tinyint NOT NULL UNSIGNED DEFAULT '0', PRIMARY KEY(coll_id), INDEX(privacy), FOREIGN KEY fk_priv (privacy) REFERENCES privacy_level (level) ON UPDATE CASCADE ON DELETE RESTRICT ) ENGINE=InnoDB; CREATE TABLE privacy_level ( level tinyint NOT NULL UNSIGNED, name varchar(20) NOT NULL, PRIMARY KEY (level) ) ENGINE InnoDB; My question is about the ON DELETE RESTRICT clause and

SQL Server 2008: The columns in table do not match an existing primary key or unique constraint

谁说胖子不能爱 提交于 2019-11-30 05:34:28
I need to make some changes to a SQL Server 2008 database. This requires the creation of a new table, and inserting a foreign key in the new table that references the Primary key of an already existing table. So I want to set up a relationship between my new tblTwo, which references the primary key of tblOne. However when I tried to do this (through SQL Server Management Studio) I got the following error: The columns in table 'tblOne' do not match an existing primary key or UNIQUE constraint I'm not really sure what this means, and I was wondering if there was any way around it? It means that

Partitioning mySQL tables that has foreign keys?

北城以北 提交于 2019-11-30 05:33:49
问题 What would be an appropriate way to do this, since mySQL obviously doesnt enjoy this. To leave either partitioning or the foreign keys out from the database design would not seem like a good idea to me. I'll guess that there is a workaround for this? Update 03/24: http://opendba.blogspot.com/2008/10/mysql-partitioned-tables-with-trigger.html How to handle foreign key while partitioning Thanks! 回答1: It depends on the extent to which the size of rows in the partitioned table is the reason for

Foreign keys in MySQL?

自作多情 提交于 2019-11-30 04:53:54
I have been slowly learning SQL the last few weeks. I've picked up all of the relational algebra and the basics of how relational databases work. What I'm trying to do now is learn how it's implemented. A stumbling block I've come across in this, is foreign keys in MySQL. I can't seem to find much about the other than that they exist in the InnoDB storage schema that MySQL has. What is a simple example of foreign keys implemented in MySQL? Here's part of a schema I wrote that doesn't seem to be working if you would rather point out my flaw than show me a working example. CREATE TABLE `posts` (