In the context of relational databases, a foreign key is a referential constraint between two tables
Questions tagged [foreign-key-relationship]
1746 questions
277
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…

taher chhabrawala
- 4,110
- 4
- 35
- 51
139
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…

Shawn Mclean
- 56,733
- 95
- 279
- 406
124
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
- 6,191
- 6
- 44
- 77
122
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
- 12,596
- 27
- 111
- 166
103
votes
2 answers
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?

user3915903
- 1,185
- 2
- 9
- 10
88
votes
3 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
…

eggdrop
- 3,356
- 5
- 29
- 32
66
votes
12 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
- 16,477
- 38
- 108
- 160
63
votes
3 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.…

Black Eagle
- 1,107
- 2
- 11
- 18
60
votes
2 answers
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
- 4,350
- 13
- 37
- 50
53
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
- 5,371
- 4
- 40
- 55
48
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…

Jack
- 1
- 21
- 118
- 236
48
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,591
- 2
- 18
- 31
43
votes
6 answers
Link in django admin to foreign key object
I have a model A with a ForeignKey to a model B.
In Django admin, how can I add a link in the admin page of model A next to the ForeignKey field which open the admin page of the model B ?

Laurent
- 1,710
- 4
- 25
- 46
41
votes
3 answers
Postgres: left join with order by and limit 1
I have the situation:
Table1 has a list of companies.
Table2 has a list of addresses.
Table3 is a N relationship of Table1 and Table2, with fields 'begin' and 'end'.
Because companies may move over time, a LEFT JOIN among them results in multiple…

Hikari
- 3,797
- 12
- 47
- 77
39
votes
5 answers
How to duplicate schemas in PostgreSQL
I have a database with schema public and schema_A. I need to create a new schema schema_b with the same structure than schema_a.
I found the function below, the problem is that it does not copy the foreign key constraints.
CREATE OR REPLACE…

Cristhian Boujon
- 4,060
- 13
- 51
- 90