我有两张桌子:
CREATE TABLE IF NOT EXISTS `mydb`.`league_ranking` (
`id` INT NOT NULL,
`position` INT NULL,
`team_id` INT NULL,
`season_id` INT NULL,
`round_id` INT NULL,
`competition_id` INT NULL,
`group_id` INT NULL,
`played_matches` INT NULL,
`wins` INT NULL,
`draws` INT NULL,
`losses` INT NULL,
`goals_for` INT NULL,
`goals_against` INT NULL,
`goals_difference` INT NULL,
`points` INT NULL,
PRIMARY KEY (`id`),
INDEX `FK_competition_groups_form_ranking_group_id_idx` (`group_id` ASC),
CONSTRAINT `FK_team_league_ranking_teamd_id`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_round_league_ranking_round_id`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`round` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_competition_league_ranking_competition_id`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`competition` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_competition_groups_league_ranking_group_id`
FOREIGN KEY (`group_id`)
REFERENCES `mydb`.`competition_groups` (`group_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`competition` (
`id` INT NOT NULL,
`country_id` INT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`link` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_idx` (`country_id` ASC),
CONSTRAINT `FK_country_competition_country_id`
FOREIGN KEY (`country_id`)
REFERENCES `mydb`.`country` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
我正在尝试在league_ranking
表中添加具有不同FK
的记录。我在competition
表中定义了一个具有以下字段值的记录:
id | country_id | name | link
13 89 foo test
我以这种方式添加记录:
string query = "INSERT INTO league_ranking (position, team_id, season_id,
round_id, competition_id, " +
"group_id, played_matches, wins, draws, losses, goals_for, goals_against, goals_difference, points) " +
"VALUES (@position, @team_id, @season_id, @round_id, @competition_id, @group_id, @played_matches, " +
"@wins, @draws, @losses, @goals_for, @goals_against, @goals_difference, @points)";
MySqlCommand = new MySqlCommand(query, MySqlConnection);
MySqlCommand.Parameters.AddWithValue("@position", tbl.Position);
MySqlCommand.Parameters.AddWithValue("@team_id", tbl.TeamId);
MySqlCommand.Parameters.AddWithValue("@season_id", seasonId);
MySqlCommand.Parameters.AddWithValue("@round_id", roundId);
MySqlCommand.Parameters.AddWithValue("@competition_id", compId);
MySqlCommand.Parameters.AddWithValue("@group_id", groupId);
MySqlCommand.Parameters.AddWithValue("@played_matches", tbl.PlayedMatches);
MySqlCommand.Parameters.AddWithValue("@wins", tbl.Wins);
MySqlCommand.Parameters.AddWithValue("@draws", tbl.Draws);
MySqlCommand.Parameters.AddWithValue("@losses", tbl.Losses);
MySqlCommand.Parameters.AddWithValue("@goals_for", tbl.GoalsFor);
MySqlCommand.Parameters.AddWithValue("@goals_against", tbl.GoalsAgainst);
MySqlCommand.Parameters.AddWithValue("@goals_difference", tbl.GoalDifference);
MySqlCommand.Parameters.AddWithValue("@points", tbl.Points);
MySqlCommand.ExecuteNonQuery();
我收到此错误:
MySql.Data.MySqlClient.MySqlException:'无法添加或更新子行:外键约束失败(
mydb
。league_ranking
,CONSTRAINTFK_competition_league_ranking_competition_id
FOREIGN KEY({{1 }})参考id
(competition
)ON更新没有动作更新没有行动''
现在id
id competition_id
的值和记录存在于13
表中,我做错了什么?
答案 0 :(得分:0)
在表mydb.league_ranking
中定义外键时,您使用错误的列来引用competition
表中的右列。
例如,不应使用id
表格中的league
,而应使用competition_id
来引用id
表格的competition
。
同样,也可以使用其他列。
相应地重新定义league
表,然后尝试插入语句。