我在子表上创建外键时遇到问题。
这是父表:
CREATE TABLE plan_det
(
plan_id INT NOT NULL,
position INT NOT NULL,
exercise_id INT NOT NULL,
sets INT NULL,
reps INT NULL,
PRIMARY KEY (plan_id, position),
CONSTRAINT fk_workout_det_workout_def1
FOREIGN KEY (plan_id) REFERENCES plan_def (id),
CONSTRAINT fk_plan_det_exercise1
FOREIGN KEY (exercise_id) REFERENCES exercise (id)
);
CREATE INDEX fk_plan_det_exercise1_idx
ON plan_det (exercise_id);
这是子表:
CREATE TABLE workout_exercise
(
workout_id INT NOT NULL,
datetime_start DATETIME NOT NULL,
plan_position INT NOT NULL,
PRIMARY KEY (workout_id, datetime_start, plan_position),
CONSTRAINT fk_workout_exercise_workout_workout_id
FOREIGN KEY (workout_id) REFERENCES workout (workout_id)
);
在此之前,一切正常。
但是当我尝试从workout_exercise.plan_position
到plan_det.position
创建一个外键时,如下所示:
ALTER TABLE workout_exercise
ADD CONSTRAINT workout_exercise_plan_det_position_fk
FOREIGN KEY (plan_position) REFERENCES plan_det (position)
我收到错误:
[HY000] [1005]无法创建表格' web39_db8。#sql-5079_138f8f9' (错误:150)
我无法解释此错误,因为两列的类型完全相同......