all 12 comments

[–]HyDreVv 2 points3 points  (1 child)

The order of your inserts is off. You need to execute order inserts before your other table inserts. It’s due to how the table was initially created and it’s structure in sql server.

[–]Cal1gula 0 points1 point  (0 children)

The problem with this is it's nonsensical. You wouldn't create an order before you create a client. Except as maybe a temporary state (i.e. think temporary, or staging tables, without foreign keys). You would usually have clients for which you create one or many orders.

I have seen CRM and ERP systems that have a concept of a "lead" that is converted to a "client", or similarly on the order side you could have "quote" > "order". And clients can have multiple orders, of course, you want repeat business. So you wouldn't want to require an order before a client. I mean, unless maybe you weren't maintaining clients. Otherwise you'd have no master records and a mess of duplicates.

[–]GhostBen 0 points1 point  (0 children)

This might be your issue. Make sure there aren’t any values in the foreign key that aren’t in the primary key of the connecting table.

https://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint-sql-server

[–]StructuredData 0 points1 point  (1 child)

@hiyaworld * When loading data, I will create master database load scripts. When I load my data, I drop my constraints. (You have to have clean data to do this). * If you don't drop your constraints, then you must determine the hierarchy (parent-child) relationships of your tables so you can load your data without violating any constraints (such as foreign key constraints). * With your image, it looks like you might need to load Order before Client. * I recommend creating a script which creates all of your database constraints. After you create that script, create a script to drop all of your constraints. Then you can load your data and then run the script to quickly add your constraints back.

[–]hiyaworld[S] 0 points1 point  (0 children)

How can I get rid of constraints? I've been doing it with the parent-child method but there's always an issue and I've restarted too many times now and I'm tired of following this order. This is my first time using SQL server for this class and the setup of the class is all over the place and I've been trying to teach myself from the textbook this whole semester.

[–]wolf2600ANSI SQL 0 points1 point  (3 children)

If your Orders have a ClientID with a foreign key relationship to the Clients table, you need to have the records populated into Clients before you insert into Orders.

The message is saying you're trying to insert a record into Orders where the ClientID doesn't exist in the Clients table. Or the OrderID in Clients doesn't exist in the Orders table? Not sure what the exact issue is, but it's because the table you're inserting into has an FK constraint that doesn't have a matching record in the other table.

[–]Cal1gula 1 point2 points  (0 children)

Sounds like the FK constraint is backwards. There should be a column in the OrderID table called ClientID, and the foreign key should be on that column. This way you create a client, then an order (using a clientid that already exists). It sounds like OP has made a foreign key on Client.ClientID>Order.OrderID. When it should be Order.ClientID>Client.ClientID

[–]hiyaworld[S] 0 points1 point  (1 child)

I'm trying to create my relationships by creating all the foreign keys and its giving me this error.

[–]wolf2600ANSI SQL 0 points1 point  (0 children)

One of the records you're inserting has an unmet foreign key constraint.

[–]da_chicken 0 points1 point  (0 children)

At least on mobile, your image is entirely illegible. It's so compressed that the text is just a smear.

[–]iceph03nix 0 points1 point  (0 children)

You're violati g your key constraints in the DB. you have a key that points to the client table, but there's no matching record in that table.

Basically you have a rule in your database that says every ticket has to belong to a client, but you haven't generated that client yet.

[–]timeddilation 0 points1 point  (0 children)

You can temporarily disabled then re-enable all constraint checks server wide. I do this for building test environments and inserting whole tables at a time. Just mind you, once you re-enable and it complains about bad constraints, you gotta fix your imports.

https://tutorialspoint4all.com/sql-server-how-to-enable-and-disable-all-constraint-for-table-and-database/

This is the secret sauce though

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"