gpt4 book ai didi

mysql - Grails 实体更改我的表并创建新表

转载 作者:行者123 更新时间:2023-11-29 13:33:16 25 4
gpt4 key购买 nike

我有 2 个表,具有一对多关系。

我在这个表上创建了 2 个实体。但是当我尝试获取创建第三个表的数据并在现有表中添加新字段时。

我的 table

CREATE TABLE `question` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`Question` VARCHAR(200) NOT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;


CREATE TABLE `answer` (
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`QuestionID` INT(11) NOT NULL,
`Answer` VARCHAR(50) NOT NULL,
`IsCorrect` TINYINT(1) NULL DEFAULT '0',
PRIMARY KEY (`ID`),
INDEX `FK_Question` (`QuestionID`),
CONSTRAINT `FK_Question` FOREIGN KEY (`QuestionID`) REFERENCES `question` (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

我的实体

class Answer {

int id

String answer

boolean isCorrect
static belongsTo = Question

static constraints = {
}

}

class Question {

int id
String question
List<Answer> answers
static hasMany = [answers : Answer]

static constraints = {
}
}

使用grails后,更改mysql表

CREATE TABLE `question` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`Question` VARCHAR(200) NOT NULL,
`version` BIGINT(20) NOT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=15;

CREATE TABLE `answer` (
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`QuestionID` INT(11) NOT NULL,
`Answer` VARCHAR(50) NOT NULL,
`IsCorrect` TINYINT(1) NULL DEFAULT '0',
`version` BIGINT(20) NOT NULL,
`is_correct` BIT(1) NOT NULL,
PRIMARY KEY (`ID`),
INDEX `FK_Question` (`QuestionID`),
CONSTRAINT `FK_Question` FOREIGN KEY (`QuestionID`) REFERENCES `question` (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=32;

CREATE TABLE `question_answer` (
`question_answers_id` INT(11) NULL DEFAULT NULL,
`answer_id` INT(11) NULL DEFAULT NULL,
`answers_idx` INT(11) NULL DEFAULT NULL,
INDEX `FK561DF237111171E9` (`answer_id`),
CONSTRAINT `FK561DF237111171E9` FOREIGN KEY (`answer_id`) REFERENCES `answer` (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

那么,如何解决这个问题呢?为什么 grails 改变我的表?

最佳答案

问题在于您声明:

static belongsTo = Question

但这使得关系成为单向的,因为您无法查询此属于。在这种情况下,Grails 将使用连接表。如果您不需要连接表,则需要通过声明将其设为双向:

static belongsTo = [question: Question]

有关此的更多信息 here .

关于mysql - Grails 实体更改我的表并创建新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com