我尝试使用 Azure DevOps 中的数据库部署作业来部署多个数据库更改。这是在 Pipeline 成功创建构建之后。数据库部署作业包含以下任务:
- 初始化作业
- 下载工件
- 删除出版物
- SQL 数据库部署
- 恢复出版物副本
- 完成工作
任务 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
部分:
关于sql-server - 使用 Azure DevOps 部署数据库期间出现错误 "The DELETE statement conflicted with the REFERENCE constraint",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000754/