트랜잭션에 '추가하거나 자식 행을 업데이트 할 수는 외래 키 제약 조건 실패'). 이것은 주위를 놀러 다니기위한 시험 일 뿐이므로 SQL 주입 보안을 놓치지 않도록주의하십시오.: 나는 세 개의 테이블, 하나는 아이 (<strong>값</strong> 및 <strong>URL</strong>을 고려할 수 있으며, 두 개의 테이블 (<strong>프로파일</strong>라고도 함) 하나의 부모를 가지고
CREATE TABLE `profiles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`profile` int(10) unsigned NOT NULL,
`url_hash` varchar(32) NOT NULL,
PRIMARY KEY (`id`,`profile`,`url_hash`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1
CREATE TABLE `urls` (
`id` int(10) unsigned NOT NULL,
`url` text NOT NULL,
`title` text,
PRIMARY KEY (`id`),
CONSTRAINT `urls_ibfk_1` FOREIGN KEY (`id`) REFERENCES `profiles` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `values` (
`id` int(10) unsigned NOT NULL,
`seller` text NOT NULL,
`seller_language` text,
`quality` tinyint(3) unsigned NOT NULL,
`note` text,
`price` int(10) unsigned NOT NULL,
`count` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `values_mtg_ibfk_1` FOREIGN KEY (`id`) REFERENCES `profiles` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I 트랜잭션으로 다음 세 가지 질의 실행있어() (FALSE 자동 커밋을 둘러싸() & 롤백() 커밋 생략)
$db->query('INSERT INTO profiles(id, profile, url_hash)
VALUES (null, ' . $profile . ', "' . md5($url) . '")')
or throwException(...);
$db->query('INSERT INTO urls(id, url, title)
VALUES (' . mysqli_insert_id($db) . ', "' . $url . '", "' . $c->data('title') . '")')
or throwException(...);
$db->query('INSERT INTO values(id, seller, seller_language, quality,
note, price, count)
VALUES (' . mysqli_insert_id($db) . ', "'. $seller .'", "'. $seller_lang .'",
'. $qual .', "'. $notice .'", '. $price .', '. $ount .')')
or throwException(..);
는
exception 'Exception' with message 'Query failed (table: values):
(1452) Cannot add or update a child row: a foreign key constraint fails
(`crawler`.`values`, CONSTRAINT `values_ibfk_1` FOREIGN KEY (`id`) REFERENCES
`profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)'
초래 나는 왜 그런지 모르겠다. 부모 테이블에 해당 id이 없다는 메시지가 나타납니다. 두 번째 쿼리가 테이블 이후로 작동하는 이유는 무엇입니까? URL은 테이블 과 같은 테이블 프로필과 동일한 외래 키 관계를 가지고 있습니까?
편집 : 고유하지 않을 수 값에서 FK에 대한 내 데이터베이스 설계에 큰 결함이있다. 내일 생각할거야. :)