简体   繁体   English

无法删除或更新父行:外键约束失败

[英]Cannot delete or update a parent row: a foreign key constraint fails

When I'm calling the following method on Visual Studio (WPF Application):当我在 Visual Studio(WPF 应用程序)上调用以下方法时:

  a_autor am = (a_autor)autoren.SelectedItem;
        if (am != null)
        {

            db.a_autor.Remove(am);
            db.SaveChanges();
            autoren.Items.Refresh();
        }

A DbUpdateException occurs and on the details the inner exception "Cannot delete or update a parent row: a foreign key constraint fails ..." is included (picture below).发生 DbUpdateException 并且在详细信息中包含内部异常“无法删除或更新父行:外键约束失败...”(下图)。

These are the SQL Statements to create the database:这些是创建数据库的 SQL 语句:

    -- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Erstellungszeit: 26. Jan 2017 um 01:56
-- Server-Version: 10.1.19-MariaDB
-- PHP-Version: 5.6.28

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Datenbank: `library`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `a_auftrag`
--

CREATE TABLE `a_auftrag` (
  `a_nr` varchar(5) NOT NULL,
  `a_rueckgabedatum` date DEFAULT NULL,
  `a_datum` date DEFAULT NULL,
  `a_geliehenVon` date DEFAULT NULL,
  `mi_mitarbeiter_id` varchar(5) DEFAULT NULL,
  `k_kunden_id` varchar(5) DEFAULT NULL,
  `m_medien_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `a_auftrag`
--

INSERT INTO `a_auftrag` (`a_nr`, `a_rueckgabedatum`, `a_datum`, `a_geliehenVon`, `mi_mitarbeiter_id`, `k_kunden_id`, `m_medien_id`) VALUES
('1', '2017-01-13', '2017-01-20', '2017-01-01', '1', '1', 1),
('2', '2017-01-01', '2017-01-21', '2017-01-02', '2', '2', 2),
('3', '2017-01-05', '2016-11-30', '2016-10-04', '3', '3', 3),
('4', '2017-01-06', '2016-07-02', '2016-08-31', '4', '4', 4),
('5', '2017-01-23', '2017-03-02', '2016-07-05', '5', '5', 5),
('6', '2016-12-06', '2017-05-19', '2016-06-21', '6', '6', 6);

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `a_autor`
--

CREATE TABLE `a_autor` (
  `at_id` varchar(5) NOT NULL,
  `at_vorname` char(20) DEFAULT NULL,
  `at_nachname` char(20) DEFAULT NULL,
  `at_gebDatum` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `a_autor`
--

INSERT INTO `a_autor` (`at_id`, `at_vorname`, `at_nachname`, `at_gebDatum`) VALUES
('1', 'Paulo', 'Coelho', '1973-11-20'),
('2', 'Ursula', 'Poznanski', '1983-05-13'),
('3', 'Elfriede', 'Jelinek', '1989-09-09'),
('6', 'Franz', 'Kafka', '1883-07-03'),
('7', 'Miguel', 'de Cervantes', '1547-09-29');

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `a_medien`
--

CREATE TABLE `a_medien` (
  `m_id` int(11) NOT NULL,
  `m_isbn` int(11) DEFAULT NULL,
  `m_titel` varchar(30) DEFAULT NULL,
  `m_anzahlExemplare` int(11) DEFAULT NULL,
  `m_rueckgabedatum` date DEFAULT NULL,
  `a_autor_id` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `a_medien`
--

INSERT INTO `a_medien` (`m_id`, `m_isbn`, `m_titel`, `m_anzahlExemplare`, `m_rueckgabedatum`, `a_autor_id`) VALUES
(1, 9655, 'Die Verwandlung', 125, '2017-01-03', '6'),
(2, 3215, 'La Galatea', 93, '2016-09-03', '7'),
(3, 6212, 'Die Strafkolonie', 256, '2015-12-27', '6'),
(4, 56963, 'Der Osman', 13, '2017-01-20', '1'),
(5, 12478, 'Erebos', 6, '2017-02-03', '2'),
(6, 93154, 'Das Martyrium der Liebe', 312, '2016-05-30', '3');

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `k_kunden`
--

CREATE TABLE `k_kunden` (
  `k_id` varchar(5) NOT NULL,
  `k_vorname` char(20) DEFAULT NULL,
  `k_nachname` char(20) DEFAULT NULL,
  `k_gebDatum` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `k_kunden`
--

INSERT INTO `k_kunden` (`k_id`, `k_vorname`, `k_nachname`, `k_gebDatum`) VALUES
('1', 'Haschat', 'Neubauer', '2016-06-17'),
('2', 'Ibrahim', 'Tatlises', '1912-04-01'),
('3', 'Ferdi', 'Tayfur', '1977-12-05'),
('4', 'Serefsiz', 'Türüt', '1988-06-02'),
('5', 'Tayyip', 'Erdogan', '2012-09-03'),
('6', 'Binali', 'Yildirim', '1978-04-30');

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `mi_mitarbeiter`
--

CREATE TABLE `mi_mitarbeiter` (
  `mi_id` varchar(5) NOT NULL,
  `mi_vorname` char(20) DEFAULT NULL,
  `mi_nachname` char(20) DEFAULT NULL,
  `mi_gebDatum` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `mi_mitarbeiter`
--

INSERT INTO `mi_mitarbeiter` (`mi_id`, `mi_vorname`, `mi_nachname`, `mi_gebDatum`) VALUES
('1', 'Hursit', 'Resit', '1994-01-12'),
('2', 'Borasit', 'Osman', '1995-05-27'),
('3', 'Yalcin', 'Yigit', '1994-06-12'),
('4', 'Onur', 'Patlak', '1981-11-02'),
('5', 'Johann', 'Preissl', '1996-01-24'),
('6', 'Franz', 'Berger', '1955-04-22');

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `a_auftrag`
--
ALTER TABLE `a_auftrag`
  ADD PRIMARY KEY (`a_nr`),
  ADD KEY `mitarbeiter` (`mi_mitarbeiter_id`),
  ADD KEY `kunden` (`k_kunden_id`),
  ADD KEY `medien` (`m_medien_id`);

--
-- Indizes für die Tabelle `a_autor`
--
ALTER TABLE `a_autor`
  ADD PRIMARY KEY (`at_id`);

--
-- Indizes für die Tabelle `a_medien`
--
ALTER TABLE `a_medien`
  ADD PRIMARY KEY (`m_id`),
  ADD KEY `autor` (`a_autor_id`);

--
-- Indizes für die Tabelle `k_kunden`
--
ALTER TABLE `k_kunden`
  ADD PRIMARY KEY (`k_id`);

--
-- Indizes für die Tabelle `mi_mitarbeiter`
--
ALTER TABLE `mi_mitarbeiter`
  ADD PRIMARY KEY (`mi_id`);

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `a_auftrag`
--
ALTER TABLE `a_auftrag`
  ADD CONSTRAINT `kunden` FOREIGN KEY (`k_kunden_id`) REFERENCES `k_kunden` (`k_id`),
  ADD CONSTRAINT `medien` FOREIGN KEY (`m_medien_id`) REFERENCES `a_medien` (`m_id`),
  ADD CONSTRAINT `mitarbeiter` FOREIGN KEY (`mi_mitarbeiter_id`) REFERENCES `mi_mitarbeiter` (`mi_id`);

--
-- Constraints der Tabelle `a_medien`
--
ALTER TABLE `a_medien`
  ADD CONSTRAINT `autor` FOREIGN KEY (`a_autor_id`) REFERENCES `a_autor` (`at_id`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Exception Details异常详情

Your problem is that your a_medien table has a foreign-key constraint to your a_autor table, which is declared here:您的问题是您的 a_medien 表对 a_autor 表有一个外键约束,在此处声明:

ALTER TABLE `a_medien`
  ADD CONSTRAINT `autor` FOREIGN KEY (`a_autor_id`) REFERENCES `a_autor` (`at_id`);

This means that your column a_autor_id in your table a_medien is referenced to your at_id in your a_autor table.这意味着表 a_medien 中的列 a_autor_id 被引用到 a_autor 表中的 at_id。 So if you try to delete an autor which Id is used in a_medien.a_autor_id column somewhere this will fail.因此,如果您尝试删除某个在 a_medien.a_autor_id 列中使用 Id 的 autor,这将失败。

You can enable ON DELETE CASCADE for your constraint so that you delete all linked a_media rows automatically by deleting their autor.您可以为您的约束启用ON DELETE CASCADE ,以便您通过删除它们的 autor 来自动删除所有链接的 a_media 行。 Otherwise you have to manually delete all a_media rows which reference your autor before you can delete the autor itself.否则,您必须手动删除所有引用您的作者的 a_media 行,然后才能删除作者本身。

UDPATE UDPATE

Use the following statements to recreate your constraint with ON DELETE CASCADE .使用以下语句通过ON DELETE CASCADE重新创建约束。

ALTER TABLE `a_medien` DROP FOREIGN KEY `autor`

After this use:使用后:

ALTER TABLE `a_medien`
   ADD CONSTRAINT `autor`
   FOREIGN KEY (`a_autor_id` )
   REFERENCES `a_autor` (`at_id`)
   ON DELETE CASCADE

There were 2 problems: The first problem was , that i had to set the constraint on delete cascade, the second problem was that i set the column values of a_autor_id to 0 , which dont exist in autor_id ( values are between 1 and 6) .有两个问题:第一个问题是,我必须在删除级联上设置约束,第二个问题是我将 a_autor_id 的列值设置为 0 ,这在 autor_id 中不存在(值介于 1 和 6 之间)。 Due to that the error "Cannot add or update a child row: a foreign key constraint fails" occured.由于出现错误“无法添加或更新子行:外键约束失败”。

So i set the values of a_autor_id to ids which are in also in the a_autoren table.所以我将 a_autoren_id 的值设置为也在 a_autoren 表中的 ids。

Thank you guys !谢谢你们 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#FrameworkCore和MySQL:无法删除或更新父行:外键约束失败 - C# FrameworkCore and MySQL: Cannot delete or update a parent row: a foreign key constraint fails “无法添加或更新子行:外键约束失败”错误 - 'Cannot add or update a child row: a foreign key constraint fails' Error 错误-无法添加或更新子行:外键约束失败 - Error - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails EF6代码优先:MySqlException:无法添加或更新子行:外键约束失败 - EF6 Code First: MySqlException: Cannot add or update a child row: a foreign key constraint fails ASP.NET:无法添加或更新子行:外键约束失败 - ASP.NET: Cannot add or update child row: a foreign key constraint fails C# - 无法添加或更新子行:外键约束失败 - C# - Cannot add or update a child row: a foreign key constraint fails ASP.NET MVC 5 EF无法添加或更新子行:外键约束失败 - ASP.NET MVC 5 EF Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:在 foreach 中执行 INSERT 时外键约束失败 - Cannot add or update a child row: a foreign key constraint fails while doing INSERT in a foreach C#:在数据库中添加对象时,出现“ MySqlException:无法添加或更新子行:外键约束失败” - C#: I'm getting a “MySqlException: Cannot add or update a child row: a foreign key constraint fails” when i am Adding an object in my database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM