question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The MERGE statement conflicted with the FOREIGN KEY constraint on v0.9

See original GitHub issue

Our setup

We have a DownloadOnly setup.

public SyncAgent CreateSyncAgent()
{
    var source = CreateSourceProvider(
        databasesConfig.DataSync.RemoteDbProvider,
        databasesConfig.DataSync.RemoteDbConnectionString);

    var target = CreateTargetProvider(
        databasesConfig.RulesDbProvider,
        databasesConfig.RulesDbConnectionString);

    var agent = new SyncAgent(target, source, new SyncOptions(), GetSetup());

    // Fix for "Invalid object name" exception https://github.com/Mimetis/Dotmim.Sync/issues/563
    agent.LocalOrchestrator.OnApplyChangesFailed(args =>
    {
        if (args.Conflict.Type == ConflictType.RemoteIsDeletedLocalIsDeleted
            && args.Resolution == ConflictResolution.ClientWins)
        {
            args.Resolution = ConflictResolution.ServerWins;
        }
    });

    return agent;
}

private SyncSetup GetSetup()
{
    var dbStructure = dbStructureClient.GetTablesAndColumns();

    var syncSetup = new SyncSetup(dbStructure.Select(x => x.Key));
    foreach (var table in syncSetup.Tables)
    {
        table.SyncDirection = SyncDirection.DownloadOnly;
        table.Columns.AddRange(dbStructure[table.TableName]);
    }

    return syncSetup;
}

Scenario

We sometimes get exceptions when we remove a record from the source db. We ran on 0.9.1 and upgraded last week to 0.9.3 but it still happens. It didn’t appear on older versions.

The previous sync was succesfull “Synchronized 8 changes in dbname database in 648ms” The next sync fails with message below.

Exception

The MERGE statement conflicted with the FOREIGN KEY constraint “FK_Parking_Badges_Parking_Drivers_DriverId”. The conflict occurred in database “dbname”, table “dbtable”, column ‘Id’. The statement has been terminated.

type: Microsoft.Data.SqlClient.SqlException assembly: Dotmim.Sync.SqlServer, Version=0.9.3.0, Culture=neutral, PublicKeyToken=null method: Dotmim.Sync.SqlServer.Builders.SqlSyncAdapter+<ExecuteBatchCommandAsync>d__13.MoveNext outerType: Dotmim.Sync.SyncException

Stacktrace

[
    {
        "assembly": "Dotmim.Sync.Core, Version=0.9.3.0, Culture=neutral, PublicKeyToken=null",
        "method": "Dotmim.Sync.LocalOrchestrator+<ApplyChangesAsync>d__7.MoveNext",
        "level": 0,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
        "level": 1,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
        "level": 2,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
        "level": 3,
        "line": 0
    },
    {
        "assembly": "Dotmim.Sync.Core, Version=0.9.3.0, Culture=neutral, PublicKeyToken=null",
        "method": "Dotmim.Sync.SyncAgent+<>c__DisplayClass53_0+<<SynchronizeAsync>b__0>d.MoveNext",
        "level": 4,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
        "level": 5,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
        "level": 6,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
        "level": 7,
        "line": 0
    },
    {
        "assembly": "Dotmim.Sync.Core, Version=0.9.3.0, Culture=neutral, PublicKeyToken=null",
        "method": "Dotmim.Sync.SyncAgent+<SynchronizeAsync>d__53.MoveNext",
        "level": 8,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
        "level": 9,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
        "level": 10,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
        "level": 11,
        "line": 0
    },
    {
        "assembly": "CapaCT.Edge.DataSync, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
        "method": "CapaCT.Edge.DataSync.Sync.SyncWorker+<SyncDatabase>d__10.MoveNext",
        "level": 12,
        "line": 87,
        "fileName": "/app/Capacity.Edge/CapaCT.Edge.DataSync/Sync/SyncWorker.cs"
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw",
        "level": 13,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess",
        "level": 14,
        "line": 0
    },
    {
        "assembly": "System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
        "method": "System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification",
        "level": 15,
        "line": 0
    },
    {
        "assembly": "CapaCT.Edge.DataSync, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
        "method": "CapaCT.Edge.DataSync.DataSyncService+<Sync>d__16.MoveNext",
        "level": 16,
        "line": 137,
        "fileName": "/app/Capacity.Edge/CapaCT.Edge.DataSync/DataSyncService.cs"
    }
]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mrtnclscommented, Mar 7, 2022

This seems to fix the issue. I see that your changed the default value recently. We’ll keep it on true.

Thanks for your support!

0reactions
mrtnclscommented, Mar 3, 2022

Ok, I’ll report back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MERGE statement conflicts with foreign key constraint
The conflict occurred in database "SUBMAN", table "dbo.SSBT", column 'TN'. The statement has been terminated. When I run the subquery by itself ...
Read more >
how to handle foreign key constraints on a merge statement?
Let's keep it simple, and just say - if it breaks by a constraint violation - just don't delete the record. How can...
Read more >
FIX: A MERGE statement may not enforce a foreign key ...
Fixes a problem in which A MERGE statement may not enforce a foreign key constraint in SQL Server 2008.
Read more >
How to Fix 'Insert Statement conflicted with FOREIGN KEY ...
Unlock the power of Microsoft .NET Core with my engaging Udemy courses! For all the details and limited-time $9.99 pricing, scroll down to ......
Read more >
Mysterious foreign key errors - SQL Server Forum
It has a foreign key relationship to OCCCODES. ... The MERGE statement conflicted with the FOREIGN KEY constraint "FK_WAGE_OCCCODES".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found