question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with ForeignKey() on column with other than default schema

See original GitHub issue

If 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:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BJMdotNETcommented, Jun 14, 2017

That “other then” in the title is really bothering me… 😉 http://writingexplained.org/then-vs-than-difference

0reactions
fubar-codercommented, Mar 31, 2018

Proposed solution in #808 implemented in release/2.0.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found