我有questions
和answers
的表,每个表有一个主键id
。两列MySQL外键:整数和字符串
而且具有以下结构的表votes
:
CREATE TABLE `answers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//whatever
PRIMARY KEY (`id`)
)
CREATE TABLE `questions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//whatever
PRIMARY KEY (`id`)
)
CREATE TABLE `votes` (
`item_model` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`item_id` int(10) NOT NULL,
`vote` int(1) NOT NULL,
KEY `item_id_model` (`item_model`,`item_id`)
)
The index on votes consists of 2 columns:
item_model: string - can have one of 2 values: 'Question' or 'Answer'
item_id: integer - references the question or answer's id
如何在votes
添加一个外键约束引用的表questions
和answers
的ID。有没有办法通过组合这两个变量来组成独特的参考(item_model
和item_id
)?
或者是我选择的糟糕做法的结构,这意味着我应该创建两个数据透视表answer_vote
和question_vote
?
发布您当前的'SHOW CREATE TABLE'输出 – e4c5