日期:2014-05-16  浏览次数:21266 次

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">
&n