Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

创建表时添加外键不成功的问题 #813

Open
evillier opened this issue Nov 13, 2023 · 0 comments
Open

创建表时添加外键不成功的问题 #813

evillier opened this issue Nov 13, 2023 · 0 comments

Comments

@evillier
Copy link

我需要建2张表,一张主表sub_device设备表,主键是id。 另一张表sub_device_data_log 设备数据日志表,他的sub_device_id需要关联到sub_device主表的id字段。
CREATE TABLE sub_device_data_log (
id int(11) NOT NULL AUTO_INCREMENT,
create_time datetime(0) NOT NULL COMMENT '创建时间',
data_value decimal(14, 6) NOT NULL DEFAULT 0.000000 COMMENT '数值',
sub_device_id bigint NOT NULL COMMENT '子设备id',
PRIMARY KEY (id) USING BTREE,
CONSTRAINT sub_device_id_key FOREIGN KEY (sub_device_id) REFERENCES sub_device (id) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci
DBPARTITION BY MOD_HASH(id) DBPARTITIONS 2 TBPARTITION BY MOD_HASH(id) TBPARTITIONS 1;

但是执行后没成功,看日志提示Caused by: java.sql.SQLException: Cannot add foreign key constraint。
如果直接去实际的物理库执行建表语句需要将外键的sub_device主表名称改为sub_device_1或者sub_device_0才能成功。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant