sql-server - 使用 Azure DevOps 部署数据库期间出现错误 "The DELETE statement conflicted with the REFERENCE constraint"

标签 sql-server azure azure-devops

我尝试使用 Azure DevOps 中的数据库部署作业来部署多个数据库更改。这是在 Pipeline 成功创建构建之后。数据库部署作业包含以下任务:

  1. 初始化作业
  2. 下载工件
  3. 删除出版物
  4. SQL 数据库部署
  5. 恢复出版物副本
  6. 完成工作

任务 1-3 成功完成,但任务 #4 因以下错误而中断:

==============================================================================
Task         : SQL Server database deploy
Description  : Deploy a SQL Server database using DACPAC or SQL scripts
Version      : 0.3.20
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://aka.ms/sqldacpacmachinegroupreadme)
==============================================================================
** Could not deploy package.
Warning SQL72012: The object [DB name] exists in the target, but it will not be dropped even though you selected the 'Generate drop statements for objects that are in the target database but that are not in the source' check box.
Warning SQL72012: The object [DB name_log] exists in the target, but it will not be dropped even though you selected the 'Generate drop statements for objects that are in the target database but that are not in the source' check box.
Warning SQL72015: The table [Table A] is being dropped, data loss could occur.
Warning SQL72015: The table [Table B] is being dropped, data loss could occur.
Error SQL72014: .Net SqlClient Data Provider: Msg 547, Level 16, State 0, Line 50 The DELETE statement conflicted with the REFERENCE constraint "[Foreign Key Name]". The conflict occurred in database "DB name", table "Table C", column 'Column name'.
Error SQL72045: Script execution error.  The executed script:
USE [DB name]
[SQL script]
...
...
##[error]Publishing to database 'DB name' on server 'Server name'.

我的问题是,SQL Server 数据库部署任务在开始部署时不会禁用外键吗?看起来它正在尝试从表“表 C”中删除数据,但另一个表(我们称之为“表 D”)依赖于它,这是有道理的,但我认为数据库部署任务会在以下情况下禁用所有外键:它开始删除表并部署更改。

最佳答案

您可能正在使用SQL Server 数据库部署 任务,对吗?数据库部署使用工具sqlpackage.exe ,在本例中,执行 Publish 操作。在链接中,您可以看到所有可以使用的各种参数。

就您而言,我怀疑您需要将 /p:IgnoreWithNocheckOnForeignKeys=True 添加到任务的 Additional Arguments 部分:

enter image description here

关于sql-server - 使用 Azure DevOps 部署数据库期间出现错误 "The DELETE statement conflicted with the REFERENCE constraint",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000754/

相关文章:

Sql Server - 在查询中选择逻辑结果

powershell - 使用 Azure Powershell 获取角色即时状态

C# 定时器填充数据库

python - pymssql 问题 - INSERT 不适用于参数

sql - 为什么这个语法无效?

azure - 如何在 Azure 中为 MSAL2 (TenantId) 配置 rust oauth2

azure - 使用 Kubernetes 的 AKS : not able to connect to cluster nodes once logged in to the cluster through azure-cli on Ubuntu

azure - 每个 Azure 目录的 Azure DevOps 组织数量是否有上限?

使用发布配置文件服务连接执行时 Azure DevOps 发布管道失败

Azure AD 与 Azure DevOps 同步