首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

Cannot add or update a child row: a foreign key constraint fails (`myznt/answer`

2012-03-31 
Cannot add or update a child row: a foreign key constraint fails (`myznt/answer`, CONSTRAINT `answe

Cannot add or update a child row: a foreign key constraint fails (`myznt/answer`, CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`qid`
MySQL5.0.15
一个问题表(question),一个回答表(answer)分别如下:
create table question
(
  qid int auto_increment not null,
  title VARCHAR(50),
  content text,
  itemid int,
  subid int,
  userid VARCHAR(50),
  grade VARCHAR(50),
  offerscore int,
  status int,
  questiontime datetime,
  clickcount int,
  acceptflag int,
  commenflag int,
  primary key (qid)
);
create table answer
(
  aid int auto_increment not null,
  quesans VARCHAR(50),
  userid VARCHAR(50),
  grade VARCHAR(50),
  anstime datetime,
  status int,
  qid int,
  primary key (aid) ,
  foreign key (qid) references question(qid) on delete cascade 
);

question表对应地hibernate3.1的映射文件为:Question.hbm.xml,内容如下:
<hibernate-mapping>
  <class name="com.iwtxokhtd.vo.Question" table="question">
  <id name="qid" type="java.lang.Integer">
  <column name="qid" />
  <generator class="native" />
  </id>
  <property name="title" type="java.lang.String">
  <column name="title" length="50" />
  </property>
  <property name="content" type="java.lang.String">
  <column name="content" length="65535" />
  </property>
  <property name="itemid" type="java.lang.Integer">
  <column name="itemid" />
  </property>
  <property name="subid" type="java.lang.Integer">
  <column name="subid" />
  </property>
  <property name="userid" type="java.lang.String">
  <column name="userid" length="50" />
  </property>
  <property name="grade" type="java.lang.String">
  <column name="grade" length="50" />
  </property>
  <property name="offerscore" type="java.lang.Integer">
  <column name="offerscore" />
  </property>
  <property name="status" type="java.lang.Integer">
  <column name="status" />
  </property>
  <property name="questiontime" type="java.util.Date">
  <column name="questiontime" length="19" />
  </property>
  <property name="clickcount" type="java.lang.Integer">
  <column name="clickcount" />
  </property>
  <property name="acceptflag" type="java.lang.Integer">


  <column name="acceptflag" />
  </property>
  <property name="commenflag" type="java.lang.Integer">
  <column name="commenflag" />
  </property>
  <set name="answers" inverse="true" table="answer">
  <key>
  <column name="qid" />
  </key>
  <one-to-many class="org.lxh.myzngt.vo.Answer" />
  </set>
  </class>
</hibernate-mapping>
answer表对应地hibernate3.1的映射文件为:Answer.hbm.xml,内容如下:
<hibernate-mapping>
  <class name="com.iwtxokhtd.vo.Answer" table="answer">
  <id name="aid" type="java.lang.Integer">
  <column name="aid" />
  <generator class="native" />
  </id>
  <many-to-one name="question" class="org.lxh.myzngt.vo.Question"
  fetch="select">
  <column name="qid" />
  </many-to-one>
  <property name="quesans" type="java.lang.String">
  <column name="quesans" length="50" />
  </property>
  <property name="userid" type="java.lang.String">
  <column name="userid" length="50" />
  </property>
  <property name="grade" type="java.lang.String">
  <column name="grade" length="50" />
  </property>
  <property name="anstime" type="java.util.Date">
  <column name="anstime" length="19" />
  </property>
  <property name="status" type="java.lang.Integer">
  <column name="status" />
  </property>
  </class>
</hibernate-mapping>

[解决办法]


Cannot add or update a child row: a foreign key constraint fails (`myznt/answer`, CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`qid`

你试图INSERT或UPDATE一个在question不存在的qid到answer表。

建议检查一下程序。

==== 思想重于技巧 ====

热点排行