Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 787953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:17:08+00:00 2026-05-14T21:17:08+00:00

Much of my sql code is generated (from POD). Now i have issue where

  • 0

Much of my sql code is generated (from POD). Now i have issue where table user_data has a FK to ref_status which points back to two different user_data. My code does the following

  1. begins a transaction
  2. looks at user_data (and adds it to a list)
  3. sees ref_status then repeats #2 with it
  4. executes the create table ref_status code

Then i get the exception

Foreign key 'FK__...__0F975522' references invalid table 'user_data'.

How do i create the two tables if they use both of eachother as a reference? i thought since it was in the same transaction it would work. I’ll also note this code works fine in sqlite with enabling FK support (supported since last month release of System.Data.SQLite). So how am i expected to create these two tables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:17:08+00:00Added an answer on May 14, 2026 at 9:17 pm

    Circular foreign keys are not really supported in SQL Server. It is possible to do it if you really want but it’s not very useful since you’ve have no way to insert any data – you can’t insert into table A because the required references in table B don’t exist and vice versa. The only way would be to create one of the tables without the FK and then add it after the second table is created. Then, to insert data, you’d need to disable one of the FKs and then re-enable it but this is a very resource intensive operation if you’ve lots of data since it will all need to be re-checked when the FK is re-enabled.

    Basically, you either have to live with incomplete declarative referential integrity or, perhaps more wisely, consider remodelling your data as @munisor suggests.

    WARNING: the following code smaple demonstrates how to create circular FKs but this really is very bad for your health! I’m sure that in the longer run, you won’t be wanting to do this. For example, simply trying to drop either of these tables after this is run is very difficult, you can’t simple DROP TABLE!

    CREATE TABLE [A]
    (
        [AId] int
            NOT NULL
            PRIMARY KEY,
        [BId] int
            NULL
            -- You can't create the reference to B here since it doesn't yet exist!
    )
    
    CREATE TABLE [B]
    (
        [BId] int
            NOT NULL
            PRIMARY KEY,
        [AId] int
            NOT NULL
            FOREIGN KEY
                REFERENCES [A]
    )
    
    -- Now that B is created, add the FK to A
    ALTER TABLE [A]
        ADD
            FOREIGN KEY ( [BId] )
            REFERENCES [B]
    
    ALTER TABLE [A]
        ALTER COLUMN [BId]
            int
            NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've written PL/SQL code to denormalize a table into a much-easer-to-query form. The code
I have a user defined function in a SQL Server 2005 database which returns
We all know T-SQL's string manipulation capabilities sometimes leaves much to be desired... I
With LINQ to SQL most likely going to not get as much active development
I need to know how much space occupies all the databases inside an SQL
There's lots of information about how cached execution plans in SQL Server eliminate much
Much related to this question , we have a scenario on my team where
I'm having a major performance issue with LINQ2SQL and transactions. My code does the
I copied a SQL Server database from one system to the next, identical setup,
I have a data view web part on a page which is displaying a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.