I have been having an issue with SQL Merge replication that I have finally been
able to repeat and was wondering if anyone can give me any advice.
I am experiencing this issue with SQL 2008, SQL 2012 and SQL 2014.
I have SQL Standard as a Publisher/Distributor
I have SQL Express as a Subscriber
The Publisher and Distributor are on the same server and in each case,
the version of SQL server on the Publisher and Subscriber are matched, that is
if the Publisher is SQL 2014, then the Subscriber is also SQL 2014.
I have a merge pull subscription between the Publisher database and the Subscriber database.
This is working correctly.
I now attempt to add two new table objects with a foreign key relationship
between them for example a customer and transaction table.
I create the tables and the foreign key reference in the Publisher database.
CREATE TABLE [dbo].[Customer](
[CustomerID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Name] [varchar] (40) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)
)
and
CREATE TABLE [dbo].[CustomerTransaction](
[CustomerTransactionID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[CustomerID] [int] NOT NULL,
[TransactionDetail] [varchar](100) NOT NULL,
CONSTRAINT [PK_CustomerTransaction] PRIMARY KEY CLUSTERED
(
[CustomerTransactionID] ASC
)
)
ALTER TABLE [dbo].[CustomerTransaction] WITH NOCHECK ADD CONSTRAINT [FK_CustomerTransaction_CustomerID_Customer] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customer] ([CustomerID])
NOT FOR REPLICATION
GO
I then use sp_addmergearticle to add these two tables to the publication
This requires a generation of a snapshot.
At the next merge replication cycle, the two tables and the foreign key constraint are
added to the subscriber database and the tables are marked for replication and
data flows between publisher and subscriber.
Provided the replication cycle that applies the snapshot with the new articles
FULLY completes without any issue then this is what happens and the system continues on happily.
IF THE MERGE AGENT JOB FAILS during the cycle that is applying the new snapshot to
the subscriber, normally after the point where the replication monitor on the publisher is
reporting "preparing table for merge replication" then when the merge agent is started again
the snapshot is applied from the start begining with the REFERENCED table first, resulting in an error
"Could not drop object 'dbo.Customer because it is referenced by a FOREIGN KEY constraint"
This seems to happen even if the shapshot has been fully applied and the process is stopped in the
middle of the "uploading data to the publisher" or "downloading data changed to the subscriber" stages.
Furthermore, it does not seem to matter in which order I add the two above tables, I still get
the same error.
My only course of action has been to drop the foreign key reference at the subscriber, but this
requires manual interverntion.
I have had a further issue in that sometimes, when I attempt to recover from this issue I find that the tables have been added to replication (sysmergeartices) on the subscriber, but the identity range management has been left off.
There is no identity range constraint on the column and the column has an initial seed of 1. When
the transactions are merged this results in conflicts. As I am unable to obtain a new identity
range my only option is to drop the article from the publication and add it back in, requiring
the table to be transferred to all subscribers.
I do want foreign keys on the subscriber tables as the whole point of merge replication is that
the subscriber databases are "live", so can anybody point me in the direction I am going wrong
in trying to add tables to my publication.
Regards, Phil Doensen