How to fix my Merge Replication upload order? I have several Merge Replication instances in production on SQL 2008 R2.  One of them has started to behave oddly in that it appears to attempting to upload the children tables before it uploads the parent.  This fails due to the foreign key of the children not having a valid parent row to reference.  Eventually the parent row is uploaded and then the children are retried, but often this can take an hour or so of retrying the rows in the children tables before the parent row is uploaded.  How can I track down and fix what is wrong with my merge replication so that it consistently uploads the parent rows before the children?
There are cases in Merge Replication when parent and child rows can be processed out of order on synchronization resulting in constraint violations and conflicts.

To understand how and why this can occur and how it may apply to your specific scenario, I recommend reading through:


How to understand Merge Replication article processing order
Merge Article Processing Order and using the "Not for Replication" Attribute


To immediately alleviate the constraint violations and conflicts from occurring on synchronization you can mark the foreign keys as NOT FOR REPLICATION.  This means that the foreign key constraint will not be enforced when the Merge Agent performs an operation.  To mark the foreign key constraint as NOT FOR REPLICATION please see Controlling Constraints, Identities, and Triggers with NOT FOR REPLICATION.

Another approach is to increase -UploadGenerationsPerBatch and the -DownloadGenerationsPerBatch Merge Agent parameters to avoid splitting parent and child changes across generation batches.