1

我有两个相互关联的表。当它尝试插入时,我收到错误消息:

无法添加或更新子行:外键约束失败(Token, CONSTRAINT FK_Token_my_aspnet_users_idFOREIGN KEY ( UserId) REFERENCES my_aspnet_users( id) ON DELETE NO ACTION ON UPDATE NO ACTION)

这是两个表的脚本:

令牌

CREATE TABLE Token (
  Id INT(11) NOT NULL AUTO_INCREMENT,
  UserId INT(11) NOT NULL,
  Token CHAR(36) NOT NULL,
  PRIMARY KEY (Id),
  CONSTRAINT FK_Token_my_aspnet_users_id FOREIGN KEY (UserId)
    REFERENCES my_aspnet_users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
)
ENGINE = INNODB
AUTO_INCREMENT = 7
CHARACTER SET latin1
COLLATE latin1_swedish_ci;

my_aspnet_users

CREATE TABLE my_aspnet_users (
  id INT(11) NOT NULL AUTO_INCREMENT,
  applicationId INT(11) NOT NULL,
  name VARCHAR(256) NOT NULL,
  isAnonymous TINYINT(1) NOT NULL DEFAULT 1,
  lastActivityDate DATETIME DEFAULT NULL,
  PRIMARY KEY (id),
  INDEX fk_users_applications_idx (applicationId),
  CONSTRAINT fk_users_applications FOREIGN KEY (applicationId)
    REFERENCES my_aspnet_applications(id) ON DELETE NO ACTION ON UPDATE NO ACTION
)
ENGINE = INNODB
AUTO_INCREMENT = 364
AVG_ROW_LENGTH = 83
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
4

1 回答 1

4

验证您尝试插入的令牌 ID 是否确实存在

于 2013-09-30T16:04:58.773 回答