Questions tagged [foreign-key-relationship]

In the context of relational databases, a foreign key is a referential constraint between two tables

1770 questions
504
votes
6 answers

What is `related_name` used for in Django?

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name='maps'? class Map(db.Model): members = models.ManyToManyField(User,…
zjm1126
  • 52,371
  • 71
  • 163
  • 213
448
votes
7 answers

When to use "ON UPDATE CASCADE"

I use "ON DELETE CASCADE" regularly but I never use "ON UPDATE CASCADE" as I am not so sure in what situation it will be useful. For the sake of discussion let see some code. CREATE TABLE parent ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY…
NawaMan
  • 23,475
  • 9
  • 48
  • 74
272
votes
3 answers

What does principal end of an association means in 1:1 relationship in Entity framework

public class Foo { public string FooId{get;set;} public Boo Boo{get;set;} } public class Boo { public string BooId{get;set;} public Foo Foo{get;set;} } I was trying to do this in Entity Framework when I got the error: Unable to…
143
votes
1 answer

postgresql foreign key syntax

I have 2 tables as you will see in my posgresql code below. The first table students has 2 columns, one for student_name and the other student_id which is the primary key. In my second table called tests, this has 4 columns, one for subject_id,…
122
votes
4 answers

Entity framework code-first null foreign key

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…
113
votes
7 answers

SQL Add foreign key to existing column

If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint: ALTER TABLE Employees ADD FOREIGN KEY (UserID) REFERENCES ActiveDirectories(id) UserID being my FK column in the Employees table. I'm trying…
ExceptionLimeCat
  • 5,577
  • 5
  • 40
  • 75
106
votes
6 answers

How to change the foreign key referential action? (behavior)

I have set up a table that contains a column with a foreign key, set to ON DELETE CASCADE (delete child when parent is deleted) What would the SQL command be to change this to ON DELETE RESTRICT? (can't delete parent if it has children)
Moak
  • 11,542
  • 26
  • 101
  • 162
81
votes
2 answers

Why can you not have a foreign key in a polymorphic association?

Why can you not have a foreign key in a polymorphic association, such as the one represented below as a Rails model? class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base …
76
votes
1 answer

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

I'm having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey?
60
votes
11 answers

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

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…
109221793
  • 15,397
  • 37
  • 100
  • 157
53
votes
2 answers

Foreign key relationship with composite primary keys in SQL Server 2005

I have two tables Table1( FileID, BundledFileID, Domain) and Table2( FileID, FileType, FileName) In Table2 FileID and FileType are the composite primary key. I want to create a foreign key relationship from Table1.FileID to Table2.…
49
votes
1 answer

SQL Sub queries in check constraint

Can I make SQL sub queries in Check constraint ? I've a post table with columns id, owner I've another table action with columns user_id, post_id Table user with columns id post_id -> post.id and user_id -> user.id also post.owner -> user.id Now I…
Dipro Sen
  • 3,590
  • 11
  • 34
  • 50
43
votes
2 answers

Defining multiple Foreign Key for the Same table in Entity Framework Code First

I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as…
43
votes
5 answers

SQLite Foreign Key

I'm following the instructions from the SQLite documentation at http://www.sqlite.org/foreignkeys.html however my attempt to add a foreign key is failing. Here are my create statements: CREATE TABLE checklist ( _id INTEGER PRIMARY KEY…
Geeks On Hugs
  • 1,531
  • 2
  • 17
  • 29
41
votes
5 answers

Foreign key referencing a 2 columns primary key in SQL Server

This question is pretty much similar to this one, but for SQL Server 2005 : I have 2 tables in my database: --'#' denotes the primary key [Libraries] #ID #Application Name 1 MyApp Title 1 2 MyApp Title 2 [Content] #ID …
Luk
  • 4,983
  • 4
  • 35
  • 54
1
2 3
99 100