Azure SQL Data Sync is a powerful service that enables data synchronization across multiple Azure SQL Databases. While synchronization failures are relatively uncommon, one error that administrators occasionally encounter is SQL Server Error 515, indicating that a NULL value cannot be inserted into a non-nullable column.
At first glance, this appears to be a straightforward data-quality problem. However, in many cases, the actual root cause lies elsewhere: inconsistencies between Azure SQL Data Sync tracking metadata and the underlying source data.
This article explains:
- Common causes of Error 515 during synchronization
- How to troubleshoot the issue
- How to identify invalid tracking records
- Safe mitigation approaches to restore synchronization
The Error
A synchronization operation may fail with an error similar to the following:
SqlException Error Code: -2146232060
SqlError Number: 515
Message:
Cannot insert the value NULL into column 'column_name',
table 'dbo.table_name';
column does not allow nulls.
INSERT fails.
SqlError Number: 3621
The statement has been terminated.
Although the error references a NULL value being inserted into a destination table, the root cause is not always missing data. In many cases, the issue originates from synchronization metadata maintained by Azure SQL Data Sync.
How Azure SQL Data Sync Tracks Changes
Azure SQL Data Sync relies on internal tracking tables to detect and replicate data changes between Hub and Member databases.
Whenever rows are inserted, updated, or deleted, synchronization metadata is recorded in tracking tables. Data Sync uses this metadata to determine what changes need to be propagated to other databases.
If the tracking metadata becomes inconsistent with the actual source table contents, Data Sync may attempt to synchronize invalid records, resulting in failures such as:
Cannot insert the value NULL into column...
Common Root Causes
Scenario 1: Schema Mismatch Between Databases
One of the most common causes of synchronization failures is a schema mismatch between synchronized databases.
For example:
| Database | Column Definition |
|---|---|
| Hub | NULL Allowed |
| Member A | NOT NULL |
| Member B | NOT NULL |
If Data Sync replicates a row containing a NULL value from the Hub database, synchronization will fail when the destination database does not allow NULL values.
Areas to Validate
Ensure the following are identical across all synchronized databases:
- Column nullability (NULL vs NOT NULL)
- Data types
- Column length
- Constraints
- Primary key definitions
Even small schema differences can cause synchronization failures.
Scenario 2: Invalid Tracking Metadata
A less obvious but frequently encountered scenario involves orphaned records in Data Sync tracking tables.
This can occur when:
- Primary key values are updated directly
- Data is modified outside expected application workflows
- Historical tracking records become disconnected from source data
- Synchronization metadata references rows that no longer exist
When Data Sync processes these stale entries, synchronization may fail with Error 515 even though the source data itself appears valid.
Troubleshooting Process
Step 1: Verify Column Definitions
Begin by examining the affected table and column identified in the error message.
For example:
sp_help 'dbo.table_name'
Review the schema on both Hub and Member databases and verify that:
- The affected column has the same definition everywhere
- NULL settings are identical
- Data types and lengths match
If discrepancies exist, align the schemas across all synchronized databases before proceeding.
Step 2: Review the Table Schema
If the schema appears consistent, review the complete definition of the affected table.
Pay particular attention to:
- Primary key columns
- Identity columns
- Constraints
- Nullable settings
Identifying the primary key is especially important for the next validation step.
Step 3: Check for Orphaned Tracking Records
Run the following query against both Hub and Member databases.
Replace:
- table_name
- primary_key
with the actual table and primary key column names.
SELECT COUNT(*)
FROM DataSync.table_name_dss_tracking t
WHERE sync_row_is_tombstone = 0
AND NOT EXISTS
(
SELECT *
FROM dbo.table_name s
WHERE t.primary_key = s.primary_key
);
For tables with composite primary keys, include all key columns in the comparison.
How to Interpret the Results
Result > 0
One or more orphaned tracking records exist.
This indicates that the tracking table contains entries that reference records no longer present in the source table.
This is a strong indicator that invalid synchronization metadata is causing the failure.
Result = 0
No orphaned records were detected.
If the synchronization error persists, further investigation should focus on schema consistency, data quality, and additional synchronization diagnostics.
Mitigation Option 1: Correct Schema Differences
If schema inconsistencies are found:
- Align the table definition across all synchronized databases.
- Ensure NULL and NOT NULL settings are consistent.
- Verify primary key definitions match.
- Reinitialize synchronization if necessary.
After schema alignment, synchronization can typically resume successfully.
Mitigation Option 2: Clean Invalid Tracking Data
If orphaned tracking records are identified, remove the invalid synchronization metadata.
Important: Always validate and test cleanup operations in a non-production environment before executing them in production.
The following query removes tracking entries that no longer correspond to records in the source table:
DELETE
FROM DataSync.table_name_dss_tracking
WHERE sync_row_is_tombstone = 0
AND NOT EXISTS
(
SELECT *
FROM dbo.table_name s
WHERE DataSync.table_name_dss_tracking.primary_key = s.primary_key
);
Replace:
- table_name
- primary_key
with the appropriate values for your environment.
After cleanup, Data Sync can rebuild valid change tracking information and synchronization typically returns to a healthy state.
Additional Validation Query
The following query can help identify historical deletion records that exist in tracking tables:
SELECT tr.id1
FROM DataSync.table2_dss_tracking tr
LEFT JOIN dbo.table2 orig
ON tr.id1 = orig.id1
WHERE tr.sync_row_is_tombstone = 1
AND orig.id1 IS NULL
AND tr.last_change_datetime >
DATEADD(day, -20, GETUTCDATE());
This can provide additional insight into how synchronization metadata is tracking deleted records.
Understanding the Underlying Cause
The most important takeaway is that the NULL value reported in the synchronization error is often not the actual problem.
A common sequence looks like this:
- A primary key value is modified directly.
UPDATE dbo.table_name
SET primary_key = new_value;
- Data Sync tracking metadata continues to reference the original key value.
- The source table and tracking table become inconsistent.
- During synchronization, Data Sync attempts to process the stale tracking record.
- The synchronization operation fails and surfaces a "Cannot insert the value NULL into column" error.
In these scenarios, cleaning invalid tracking records resolves the inconsistency and restores successful synchronization.
Best Practices to Prevent Recurrence
To minimize the likelihood of synchronization failures:
- Keep schemas identical across all synchronized databases
- Avoid updating primary key values whenever possible
- Use surrogate keys for synchronized tables
- Validate schema consistency before deploying schema changes
- Periodically investigate Data Sync tracking tables when troubleshooting synchronization failures
- Test schema modifications in non-production environments before deployment
Conclusion
When Azure SQL Data Sync reports a:
Cannot insert the value NULL into column...
error, it is important not to assume that the problem is caused by missing data in the source table.
A structured troubleshooting approach should include:
- Verifying schema consistency across synchronized databases
- Reviewing primary key definitions
- Investigating Data Sync tracking tables for orphaned records
- Cleaning invalid synchronization metadata when appropriate
In many real-world cases, stale tracking records are the true root cause. Identifying and removing these invalid entries can restore synchronization quickly and avoid unnecessary application or schema changes.
Have you encountered similar Azure SQL Data Sync issues in your environment? Share your experience and troubleshooting techniques in the comments below.