找不到MySQL外键,即使它存在

时间:2018-08-30 15:40:59

标签: mysql

我快疯了,我有2个表“ open_invoices”和“ paid_invoices”,其结构如下:

CREATE TABLE `open_invoices` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `unique_identifier` varchar(255) NOT NULL,
  `insert_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `cust_nbr` int(11) NOT NULL,
  `location` varchar(10) NOT NULL,
  `company` varchar(10) NOT NULL,
  `invoice_nbr` int(11) NOT NULL,
  `future_letter` varchar(1) NOT NULL,
  `invoice_total` decimal(8,2) NOT NULL,
  `invoice_payments` decimal(8,2) NOT NULL,
  `record_type` int(11) NOT NULL,
  `terms_desc` varchar(255) NOT NULL,
  `due_date` varchar(8) NOT NULL,
  `discount_date` varchar(8) NOT NULL,
  `orig_disc_avail` decimal(8,2) NOT NULL,
  `cust_po` varchar(10) NOT NULL,
  `invoice_date` varchar(8) NOT NULL,
  `status` int(11) NOT NULL,
  `check_no` varchar(10) NOT NULL,
  `last_pay_date` varchar(8) NOT NULL,
  `as_of_date` varchar(8) NOT NULL,
  `as_of_time` varchar(11) NOT NULL,
  `remaining` varchar(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_identifier` (`unique_identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `paid_invoices` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `date_change` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `invoice_date` varchar(8) NOT NULL,
    `unique_identifier` varchar(255) NOT NULL,
    `amount_paid` decimal(8,2) NOT NULL,
    `amount_left` decimal(8,2) NOT NULL,
    `payment_type` varchar(2) NOT NULL,
    `last4` varchar(255) NOT NULL,
    `transac_id` varchar(255) NOT NULL,
    `customer_id` varchar(255) NOT NULL,
    `payment_P_F` varchar(1) NOT NULL, 
PRIMARY KEY (`id`),
FOREIGN KEY (unique_identifier)
    REFERENCES open_invoices (unique_identifier) 
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我正尝试在“ paid_invoices”中插入一行,如下所示:

INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')

我收到以下错误:

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`accounts_receivables`.`paid_invoices`, CONSTRAINT `paid_invoices_ibfk_1` FOREIGN KEY (`unique_identifier`) REFERENCES `open_invoices` (`unique_identifier`))

INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')

我在google上搜索,它说在表'open_invoices'中找不到外键126_89948_2576。

让我发疯的是,当我SELECT * FROM open_invoices WHERE id = 2576时,看到带有外键126_89948_2576的行,如下图所示: query 1 但是当我执行此查询SELECT * FROM open_invoices WHERE unique_identifier = '126_89948_2576'时,没有任何结果,请参见以下捕获:

query 2

我确定为外键126_89948_2576保存的值中没有多余的空格。

请问发生了什么事?我使用的MySQL版本是否存在错误?

Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.6.40 - MySQL Community Server (GPL)
Protocol version: 10 

谢谢。

2 个答案:

答案 0 :(得分:4)

似乎您在查询中使用了错误的分隔符,“ _”而不是“-”。尝试unique_identifier = '126-89948-2576'

答案 1 :(得分:1)

因为下划线_是类似于%的通配符,只不过它只查找一个字符。

SQL模式匹配使您可以使用“ _”来匹配任何单个字符,使用“%”来匹配任意数量的字符(包括零个字符)。

您可以使用

选择*从打开发票的唯一标识符(例如'%126 \ _89948 \ _2576%')