Hello,
We have an SQL Server 2008 R2 database (several, in fact) that we use Merge Replication on with multiple subscribers. Several of the replicated tables have a foreign key relationship to each other.
As an example, imagine that we have a "Login" table that contains a "user_id" column, which is a foreign key to the "User" table where "user_id" is the primary key.
Both User and Login entries can be created on a subscriber, and are expected to be uploaded to the publisher when the former synchronises (which is done manually). What we observe is that when both a User and Login entry have been created on a subscriber,
the User entry is uploaded normally but there is an "Upload insert failed" conflict on the Login entry.
Researching further, I've noticed that although on the Publisher, the "Enforce for replication" setting is set to True on all FKs on the publisher, all FKs on a subscriber have it set to False. This happens immediately after initialisation on a
new subscriber.
It seems that, with the setting set to False, table synchronisation is done alphabetically and so Login fails since the User its FK refers to hasn't been uploaded yet.
The same problem has been noted in this post: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/7652d18a-fe14-434b-b96b-9dfee099a150/constraint-violation-in-merge-replication#61d1abfd-b5a0-4be0-a566-d3bd79c8db25
However no explanation has been offered.
We could of course create a script that finds all foreign keys, drops them and re-creates them without specifying "NOT FOR REPLICATION", as this is a behaviour we obviously do not want. However we'd prefer it if there was a way to ensure the FKs
are created correctly on subscribers to begin with.
Is this a bug in SQL server that still hasn't been fixed, or is there a replication setting that prevents or causes this behaviour?