Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow widening the foreign key compatibility criteria #24007

Closed
roji opened this issue Jan 28, 2021 · 2 comments
Closed

Allow widening the foreign key compatibility criteria #24007

roji opened this issue Jan 28, 2021 · 2 comments

Comments

@roji
Copy link
Member

roji commented Jan 28, 2021

ForeignKey.AreCompatible requires that the foreign and principal keys' CLR properties correspond exactly. While SQL Server is indeed very strict about that, other database support wider foreign key types pointing to narrower principal keys.

On SQL Server:

CREATE TABLE posts (id INT PRIMARY KEY);
CREATE TABLE polls (post_id BIGINT REFERENCES posts(id));

... errors with Column 'posts.id' is not the same data type as referencing column 'polls.post_id' in foreign key 'FK__polls__post_id__5165187F'..

However, the same DDL works fine on PG and Sqlite. We could allow some sort of provider hook that determines whether the properties types are compatible.

Originally flagged by @jessicah in npgsql/efcore.pg#1515

@roji
Copy link
Member Author

roji commented Feb 1, 2021

Duplicate of #19793

@roji roji marked this as a duplicate of #19793 Feb 1, 2021
@roji
Copy link
Member Author

roji commented Feb 1, 2021

Although it is possible to make EF Core support different types for principal and foreign keys, that's likely quite difficult to do. On the other hand, as specified in #19793, it's possible to simply specify the different store type in the model.

@jessicah although this wouldn't unblock the scaffolding scenario, try to simply define your foreign key as an INT in your model (instead of BIGINT) and everything should work.

@roji roji closed this as completed Feb 1, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants