Issue with ForeignKey() on column with other than default schema
See original GitHub issueIf you use the .ForeignKey method extension for .WithColumn then it uses the default schema and I’m not able to change the schema with .InSchema.
Example:
Create.Table("Company").InSchema("Foo")
.WithColumn("Id").WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
.WithColumn("Name").AsString().NotNullable()
Create.Table("Users").InSchema("Foo")
.WithColumn("Id").WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
.WithColumn("Name").AsString().NotNullable()
.WithColumn("Company_Id").AsForeignKey("Company", "Id").InSchema("Foo");
If I try to run this then I get the following error message:
!!! An error occured executing the following sql:
ALTER TABLE [Foo].[Users] ADD CONSTRAINT [FK_Users_Company_Id_Company_Id] FOREIGN KEY ([Company_Id]) REFERENCES [dbo].[Company] ([Id])
The error was Foreign key 'FK_Users_Company_Id_Company_Id' references invalid table 'dbo.Company'.
Could not create constraint. See previous errors.
!!! Foreign key 'FK_Users_Company_Id_Company_Id' references
invalid table 'dbo.Company'.
I know you can use the Create.ForeignKey() instead as workaround but I’d like the other method to work too since it has been implemented and can be used.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:5 (3 by maintainers)
Top Results From Across the Web
3 common foreign key mistakes (and how to avoid them)
Foreign key constraints are important to any SQL database, but they can also cause problems if they're not implemented correctly.
Read more >Foreign Key to non-primary key - sql
It is perfectly fine to use a foreign key as the primary key if the table is connected by a one-to-one relationship, not...
Read more >Commonly used SQL Server Constraints: FOREIGN KEY, ...
The FOREIGN KEY constraint identifies the relationships between the database tables by referencing a column, or set of columns, in the Child ...
Read more >How to add a column with a foreign key constraint ...
The FOREIGN KEY (aka parent ) column has to already exist in order to make it an FK . I did the following...
Read more >13.1.20.5 FOREIGN KEY Constraints
A foreign key constraint on the base column of a stored generated column cannot use CASCADE , SET NULL , or SET DEFAULT...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That “other then” in the title is really bothering me… 😉 http://writingexplained.org/then-vs-than-difference
Proposed solution in #808 implemented in release/2.0.0