无法解析接近的表名

问题描述 投票:16回答:3

我想创建一个对外表的引用。但我收到以下错误:

查询:

CREATE TABLE category_ids (id INT, post_id INT,
                    INDEX par_ind (post_id),
                    FOREIGN KEY (post_id) REFERENCES post(id)
                      ON DELETE CASCADE
) ENGINE=INNODB;

SHOW ENGINE INNODB状态\ G:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-08-23 00:11:06 7f6f49e7b700 Error in foreign key constraint of table fun/category_ids:
FOREIGN KEY (post_id) REFERENCES post(id)
                      ON DELETE CASCADE
) ENGINE=INNODB:
Cannot resolve table name close to:
(id)
                      ON DELETE CASCADE
) ENGINE=INNODB

发表表结构

    mysql> describe post;
    +-------------+-----------------------+------+-----+---------------------+----------------+
    | Field       | Type                  | Null | Key | Default             | Extra          |
    +-------------+-----------------------+------+-----+---------------------+----------------+
    | id          | int(11)               | NO   | PRI | NULL                | auto_increment |
...
    +-------------+-----------------------+------+-----+---------------------+----------------+
    22 rows in set (0.00 sec)
mysql sql reference
3个回答
40
投票

只有InnoDB支持外键,MyISAM不支持。即使它会,你也不能在不同类型的表之间建立关系。

因此,您需要将表post转换为InnoDB。 ALTER TABLE post ENGINE = InnoDB;


0
投票

对我来说,这适用于此。

CREATE TABLE category_ids (id INT, post_id INT references post(id),
                INDEX par_ind (post_id)
) ENGINE=INNODB;

0
投票

当父表被分区时,也会出现此错误。从父表中删除分区允许创建外部约束而没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.