table included in merge replication has all its data disposed when execute - sql-server

I have merge replication between two databases, "db1" as a publisher and "db2"
as a subscriber.
Just adding one row to "table1" included in db2 and then right clicking on data then execute I got all its data disposed. Even if I try to copy its data manually from table1 in the published I got a message that table1 is being upgraded by the publisher and I cannot insert.
The table's primary key is foreign key in other tables.
How is the content permitted to delete?

Related

How to keep foreign tables imported via foreign data wrapper in Postgres up to date with underlying table?

I am wondering how foreign data in Postgres are updated. I understand that the FDW makes a connection to the remote database to access the tables, and the foreign tables don't take up physical memory in the database. It is the equivalent of making a select statement to the the underlying tables.
I have some tables in Database A that I have imported as foreign tables into database B. I am using the foreign Tables in Database B to create materialized views.
I am running these commands in Database B:
IMPORT FOREIGN SCHEMA completed LIMIT TO (tb1,tbl2)
FROM SERVER db_data_pipeline
INTO public;
COMMIT;
CREATE MV1 AS SELECT * FROM TB1;
My tables in database A are updated through a drop and recreate process. I have two questions regarding how foreign data are synchronized:
Since my tables in database A are updated through a drop and recreate, are the foreign tables in Database B also updated automatically? is there a scenario where the foreign data becomes 'stale'? Would I have to re-import foreign tables to keep them up to date?
I've read that the foreign table definition does not change when the underlying table changes. For example, a column added in the underlying table will not be reflected in the foreign table. However, since my underlying table is never updated directly and dropped and recreated instead, do I have to worry about this issue?
Ad 1:
When you query tb1 in database B, PostgreSQL composes a query, connects to database A and runs the query. If the table in database A has been dropped and re-created, this query will access the new table, since it goes by the table name. There is no need to drop and re-create the foreign tables. The data in the foreign tables will never be stale, but the data in the materialized views can of course become stale.
Ad 2:
Yes, you have to worry about that. A change in the table definition on database A will not change the foreign table in database B. So if you add a bew column in database A, it will not show up in the foreign table in database B. If you drop a column in the table in database A, querying the foreign table may cause an error, since the query could access the missing column. You have to modify or re-create the foreign table to deal with metadata changes.

sql replication error - record exists on Publisher but it's trying to delete/insert that on Subscriber, so Foreign Key/Primary Key violation

We have transactional one-way replication running, and suddenly today started getting this error :
The DELETE statement conflicted with the REFERENCE constraint "FK_BranchDetail_Branch".
The conflict occurred in database "LocationDB", table "dbo.BranchDetail", column 'BranchNumber'.
Message: The row was not found at the Subscriber when applying the replicated command.
Violation of PRIMARY KEY constraint 'PK_Branch'. Cannot insert duplicate key in object
'dbo.Branch'. The duplicate key value is (23456)
Disconnecting from Subscriber 'SQLDB03'
Publisher - SQLDB02.LocationDB
Subscriber - SQLDB03.LocationDB
Tables on both servers:
Branch (BranchNumber PrimaryKey)
BranchDetail (BranchNumber ForeignKey references previous table)
select * from SQLDB02.LocationDB.Branch -- contains : 23456,'Texas',...
select * from SQLDB03.LocationDB.Branch -- contains : 23456,'NULL',...
The problem is - the BranchNumber in question '23456' exists in all 4 tables (Publisher..Branch, Publisher..BranchDetail, Subscriber..Branch, Subscriber..BranchDetail).
Yet, when I ran a trace on Subscriber, I see repeated commands like:
exec [sp_MSdel_dboBranch] 23456 -- which throws FK violation
exec [sp_MSins_dboBranch] 23456,'NULL',... -- which throws PK violation
I'm guessing it's trying to Update the record on subscriber by doing a Delete + Insert. But it's unable to..
Users do not have access to modify Subscriber table. But they can modify Publisher table through UI, and have been doing so for long time without issue. There is also job that updates Publisher table once every night. We started getting this error around noon today.
Our last resort is to reinitialize subscription off-hours.
But any ideas what could have caused it and how to fix it?
For transactional replication, updating primary key column is replicated as delete + insert (deferred update). because this PK column has an FK constraint, the delete will fail at the subscriber. You have a couple workarounds to prevent this from happening moving forward:
Disable replicating of FK constraints, as it is not really needed for one-way replication. Why? Users are not entering data at the subscriber, so there is no need to maintain referential integrity, and transactional Replication replicates log txns, so the order of txns is pretty much guaranteed, no need to worry about one txn showing up before another.
Enable trace flag 8207 on the publisher. If only a single row is updated, then it will be replicated as a single UPDATE statement. If the update affects multiple rows, then it will be replicated as deferred update.
Somehow block users from updating PKs
IMO, best bet is the first option.
How to fix this? Reinit is one way. But if you can manaully disable or drop the FK constraint on the subscriber, that is easiest solution.

Remove record from sys.objects?

I ran a ALTER SCHEMA ... on a table (SQL Server 2012 SP1) but the sys.objects record is still there. When I run the ETL I get an error that the table doesn't exist anymore because it's trying to remove all constraints. This one is of type PK Primary Key Constraint. How can I safely remove the record from the sys.objects table?
I managed to fix this issue by scripting the database to a new one, ALTER SCHEMA ... again on that table to bring it back to dbo., then DROP and recreate the table in the different table schema.

SQL Server foreign key reference to 2 other primary keys

I have two tables:
Table1 with composite primary key - PRID, SPRID, STIM and
Table2 with primary key - MTID
I need the SPRID field to reference either PRID or MTID. I have seen examples where this can work if the value you want to put in SPRID is in both PRID and MTID but haven't yet seen any where it is only required to be in either. I have set cascade update and delete to ON and want this behavior for the finished design.
I have tried creating an intermediate table that is populated with PRID and MTID in a single field via triggers in both tables but it only seems to populate the MTID entries. When I try to enter data in Table 1 (in a view of Table1) I get the following message:
"This row was successfully committed to the database. However, a problem occurred when attempting to retrieve this record from the data back after the commit. ..."
When I re-query the record disappears. If I switch the FK constraint off I can enter data that complies with the constraints but as soon as I switch the constraint back on the data is deleted.
I can't change the db design as these tables are part of a commercial piece of software that I am trying to integrate with my SQL back end but am open to any suggestions of a suitable work around.
PRID and MTID are of nvarchar type (as is SPRID), both tables are in the same schema but you cannot have a PRID value that is the same as the MTID value.

SQL Server merge replication causes foreign key failures

In my application, using SQL Server 2005, I'm having two tables, let's call them Table A and Table B; a foreign key constraint is defined on Table B, referencing the primary-key column in Table A, which is an auto-generated integer ID. I'm running the following simple transaction:
Start transaction
Insert a row to table A
Retrieve the last-generated ID ("SELECT ##IDENTITY ... ")
Insert data to table B, using this ID
Commit
It all works well, until I'm trying to create merge replication (continuous) with another SQL Server 2005. Both publisher and subscriber now fail this transaction when trying to insert data to table B, because of foreign key constraint failure:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TableB_TableA". The conflict occurred in database "MyDB", table "TableA", column 'ID'.
I was not able to make it work by committing after inserting data to table A. However, after removing the merge replication, everything worked. The database code is written in C++, using ADO.
Is the replication interfering with the transaction anyhow? Any other possible explanation?
Are the Primary Key values on Table A at both server nodes discrete from one another (In other words are you using Identity Range management at each node)?
Also, has the Foreign Key constraint been configured with the Not For Replication property?
I would assume that because your Foreign Key constraint has already been enforced locally at the Publisher, that you do not need to re-check it when merging with the Subscriber.
Looks like the issue is related to the scope of the ##IDENTITY function. When I used LAST_IDENT('TableB') instead, things seem to work.
As described in MSDN:
IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
##IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Resources