Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Replication (2008)
 Adding foreign key constrain fails on subcriber

Author  Topic 

palmsnow
Starting Member

5 Posts

Posted - 2011-04-28 : 14:50:43
have a replicated database using SQL Server 2008. Here is what I am trying to do.
1.Create a new table (MyNewTable)
2.Create a column in an existing Table (MyExistingTable)
3.Create a FK constraint on this column (in #2) as it points to PK to new table from #1.

However when data get replicated to subscriber, i get following error in replication monitor.


The schema script "script for adding foreign key constraint" could not be propagated to the subscriber. (Source: MSSQL_REPL, Error number: MSSQL_REPL-2147201001) Get help: http://help/MSSQL_REPL-2147201001 Foreign key "MyFKConstraint" references invalid table "MyNewTable" (Source: MSSQLServer, Error number: 1767)

This script runs fine on publisher though. Some people seem to suggest that adding foreign key constraint cannot be done in a replicated environment when its created between an existing table and a new table. This is kind of hard to buy for me although I am kind of new to SQL Server replication. Any thoughts?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-28 : 15:00:01
We don't replicate our foreign keys. If you want to add the constraint, how about you just run the statement on the subscriber to get it created? Please ensure that all of your articles are together that have relationships, otherwise you're going to have problems.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-04-28 : 15:05:48
"We don't replicate our foreign keys."

We don't either.
Go to Top of Page

palmsnow
Starting Member

5 Posts

Posted - 2011-04-28 : 15:31:02
" Please ensure that all of your articles are together that have relationships, otherwise you're going to have problems".

Our current process is that we just run the script on the publisher and it get replicated to subscriber. Seem like on subscriber things are not replicated in the order we are doing it on publisher. As per your suggestion above, how can we ensure it?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-28 : 16:16:35
If they aren't happening in the right order, then it sounds like you've got multiple publications. All related articles need to be in the same publication to avoid FK issues.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

palmsnow
Starting Member

5 Posts

Posted - 2011-04-29 : 10:04:12
We have just one publisher and several subscribers. I guess my question is really to verify what our DBA is saying about why the problem is happening at first place(because its hard for me to belive).
My script is in following order.

Step 1
CREATE TABLE MyNewTable
ID INT IDENTITY(1,1) NOT NULL
........
........

Step 2
ALTER TABLE MyExistingTable ADD IDFromMyNewTable INT

Step 3
ALTER TABLE MyExistingTable ADD CONSTRAINT FK_BlahBlah FOREIGN KEY(IDFromMyNewTable) REFERENCES MyNewTable (ID)

This scripts work fine on pubisher but it fails on Subscriber compalining that FK constraint cannot be added because MyNewTable does not exists. Per my DBA "its not guaranteed that these scripts will run with the same order on subscriber and step 3 can get executed before step 1 there". Is my DBA statment correct that subscriber can run script in different order then how it was executed on publisher?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-29 : 12:56:55
I don't think your DBA is right. We don't replicate our foreign keys. I don't see why anyone would actually.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

palmsnow
Starting Member

5 Posts

Posted - 2011-04-29 : 13:02:16
so what exactly will be the process of doing it. When a FK is created on publisher it gets replicated to subscriber. How can I make FKs not get replicated to subscribers? Also is there any best practice document describing how to handle this type of scenario?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-29 : 13:33:31
The articles will need to be updated to turn off FK replication.

The default is actually off for this, so your DBA must've turned it on.

As for best practices, I haven't seen anything.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

palmsnow
Starting Member

5 Posts

Posted - 2011-04-29 : 16:59:33
So just to be sure if I got it correct, you have FKs replication off for all tables ( existing one or new ones)?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-30 : 00:13:42
Yes for all tables. The default is off, and we leave it at the default.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -