简体   繁体   中英

Programmatically display column names violating MySQL unique constraint with PHP

When MySQL returns 23000 error code, it means that the update/insert query has triggered integrity constraint violation. However, the error, eg

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '22-offline-mpu-l' for key 'client_ad'

However, how to tell what are the columns that require unique combination?

The above table schema is:

CREATE TABLE `ad` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `client_id` smallint(5) unsigned NOT NULL,
  `slug` varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT '',
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `status` tinyint(3) unsigned NOT NULL,
  `width` smallint(5) unsigned NOT NULL,
  `height` smallint(5) unsigned NOT NULL,
  `code` varchar(2040) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `client_ad` (`client_id`,`slug`),
  CONSTRAINT `ad_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `client` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1746 DEFAULT CHARSET=utf8;

This is a proposal, rather than a definitive solution:

try {
    // Query
} catch (\PDOException $e) {
    if ($e->getCode() === '23000') {
        // SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '22-offline-mpu-l' for key 'client_ad'
        preg_match('/(?<=\')[^\']*(?=\'[^\']*$)/', $e->getMessage(), $match);

        $columns = $this->db->prepare("
        SELECT
            `f1`.`name`
        FROM
            `information_schema`.`innodb_sys_tables` `t1`
        INNER JOIN
            `information_schema`.`innodb_sys_indexes` `i1` USING (`table_id`)
        INNER JOIN
            `information_schema`.`innodb_sys_fields` `f1` USING (`index_id`)
        WHERE
            `t1`.`schema` = DATABASE() AND
            `t1`.`name` = :table_name AND
            `i1`.`name` = :key_name AND
            `i1`.`type` IN (2, 3)
        ORDER BY
            `f1`.`pos`;
        ")
            ->execute(['table_name' => static::$table_name, 'key_name' => $match[0]])
            ->fetchAll(\PDO::FETCH_COLUMN);

        // $columns contains names of the columns that make up the index with the integrity constraint.
    } else {
        throw $e;
    }
}

This uses regular expression to match the key name, then looks up the associated columns with the database, table and key name combination. innodb_sys_indexes.type 2 and 3 are the only ( http://dev.mysql.com/doc/refman/5.6/en/innodb-sys-indexes-table.html ) keys that (apply to tables) require unique values across the table.

It looks like the problem is with the foreign key referencing the client table, so I would do:

SHOW INDEXES FROM client WHERE Non_unique = 0;

and look at the columns you get back.

http://dev.mysql.com/doc/refman/5.0/en/show-index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM