【问题标题】:The duplicate key value is (<NULL>, <NULL>)重复键值为 (<NULL>, <NULL>)
【发布时间】:2022-01-27 12:27:01
【问题描述】:

所以我正在尝试将表从 MySQL 迁移到 MSSQL (sql server migration assistant MySQL),但我收到此错误:

Migrating data...
Analyzing metadata...
Preparing table testreportingdebug.testcase...
Preparing data migration package...
Starting data migration Engine
Starting data migration...
The data migration engine is migrating table '`testreportingdebug`.`testcase`': > [SwMetrics].[testreportingdebug].[testcase], 8855 rows total
Violation of UNIQUE KEY constraint 'testcase$Unique'. Cannot insert duplicate key in object 'testreportingdebug.testcase'. The duplicate key value is (<NULL>, <NULL>).
 Errors: Violation of UNIQUE KEY constraint 'testcase$Unique'. Cannot insert duplicate key in object 'testreportingdebug.testcase'. The duplicate key value is (<NULL>, <NULL>).
Completing migration of table `testreportingdebug`.`testcase`...
Migration complete for table '`testreportingdebug`.`testcase`': > [SwMetrics].[testreportingdebug].[testcase], 0 rows migrated (Elapsed Time = 00:00:00:01:352).
Data migration operation has finished.
    0 table(s) successfully migrated.
    0 table(s) partially migrated.
    1 table(s) failed to migrate.

我刚刚从我的表中复制了三行,它们看起来是这样的:

'1',   'Pump# TimeToService',           NULL, NULL, 'A general test case comment ...', '0'
'2',   'Config.SlaveMinimumReplyDelay', NULL, NULL, NULL,                              '0'
'3',   'Config.RESERVED',               NULL, NULL, NULL,                              '0'

如果您想知道 MySQL 表中的冒号是如何设置的,请看这里:

是因为right, left and comment可以为空吗?

表的 DDL

CREATE TABLE `testcase` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `TestCaseName` varchar(150) DEFAULT NULL,
  `Left` int(11) DEFAULT NULL,
  `Right` int(11) DEFAULT NULL,
  `Comment` text,
  `Hidden` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `Unique` (`Left`,`Right`)
) ENGINE=InnoDB AUTO_INCREMENT=10580 DEFAULT CHARSET=utf8

【问题讨论】:

  • MySQL 不会将 NULL 值视为 UNIQUE 约束中的重复项。 dev.mysql.com/doc/refman/8.0/en/… “唯一索引允许包含 NULL 的列有多个 NULL 值。” 而不是 SQL Server。 docs.microsoft.com/en-us/sql/relational-databases/tables/… “UNIQUE 约束允许值为 NULL。但是,与参与 UNIQUE 约束的任何值一样,每列只允许一个空值”
  • @Akina 我只有在未设置为唯一的列中有 NULL 值,所以为什么会失败?
  • 询问您的模型和迁移代码。
  • 为表格发布 DDL,而不是图片的片段。
  • 你有UNIQUE KEY Unique (Left,Right),我看到这些左右列是NULL。

标签: mysql sql-server database-migration


【解决方案1】:

必须删除 Unique 部分,因为它们只是 NULL。

ALTER TABLE `testreportingdebug`.`testcase` 
DROP INDEX `Unique`;

【讨论】:

    【解决方案2】:

    如果你想在 SQL Server 中严格等价于你的 MySQL 表,你必须像这样创建它:

    CREATE TABLE testcase (
      id int NOT NULL IDENTITY PRIMARY KEY,
      TestCaseName varchar(150),
      [Left] int,
      [Right] int,
      Comment VARCHAR(max),
      [Hidden] tinyint DEFAULT 0,
    );
    CREATE UNIQUE INDEX X_testcase_right_left 
       ON testcase ([Left], [Right])
       WHERE [Left] IS NOT NULL
         AND [Right] IS NOT NULL;
    

    顺便说一句,列名“Right”、“left”、“hidden”是 SQL / MS SQL Server 的保留字,不应在任何时候用于 SQL 标识符(表名、列名、proc 名...... )

    完整名单可在here获取

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2023-03-10
      • 1970-01-01
      • 2017-07-03
      • 2018-01-25
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多