- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在Grails(父子)的两个域之间创建双向关系,但是我似乎无法使其正常工作。
根据Grails GORM documentation oneToMany的说明,我应该能够在 parent 与 child 之间创建hasMany(Parent)和belongsTo(Child)来创建双向关系,但这对我不起作用。
我有以下两个域:
class Game {
String name
String description
Double price
}
class Review {
static belongsTo = [game: Game]
String reviewText
Date reviewDate
}
grails dbm-gorm-diff file.groovy
我得到以下文件
databaseChangeLog = {
changeSet(author: "efx (generated)", id: "1456032538941-1") {
createTable(tableName: "game") {
column(name: "id", type: "int8") {
constraints(nullable: "false", primaryKey: "true", primaryKeyName: "gamePK")
}
column(name: "version", type: "int8") {
constraints(nullable: "false")
}
column(name: "description", type: "varchar(255)") {
constraints(nullable: "false")
}
column(name: "name", type: "varchar(255)") {
constraints(nullable: "false")
}
column(name: "price", type: "float8") {
constraints(nullable: "false")
}
column(name: "reviews_id", type: "int8") {
constraints(nullable: "false")
}
}
}
changeSet(author: "efx (generated)", id: "1456032538941-2") {
createTable(tableName: "review") {
column(name: "id", type: "int8") {
constraints(nullable: "false", primaryKey: "true", primaryKeyName: "reviewPK")
}
column(name: "version", type: "int8") {
constraints(nullable: "false")
}
column(name: "review_date", type: "timestamp") {
constraints(nullable: "false")
}
column(name: "review_text", type: "varchar(255)") {
constraints(nullable: "false")
}
}
}
changeSet(author: "efx (generated)", id: "1456032538941-4") {
createSequence(sequenceName: "hibernate_sequence")
}
changeSet(author: "efx (generated)", id: "1456032538941-3") {
addForeignKeyConstraint(baseColumnNames: "reviews_id", baseTableName: "game", constraintName: "FK_jnjkmccicsjmsvqub534xcnnm", deferrable: "false", initiallyDeferred: "false", referencedColumnNames: "id", referencedTableName: "review", referencesUniqueColumn: "false")
}
grials dbm-update
以便将更改转移到数据库,但是我想使这种关系成为双向,因此我用“hasMany”更新了我的游戏域,如下所示
class Game {
static hasMany = [reviews: Review]
String name
String description
Double price
}
grails dbm-gorm-diff fileupdated.groovy
,以便最终创建双向关系,但得到一个空的迁移文件
databaseChangeLog = {
}
groovy:000> g = new com.pluralsight.Game([description: 'Game Desc', name: 'The Game', price: '1.99'])
===> com.pluralsight.Game : (unsaved)
groovy:000> g.save()
===> com.pluralsight.Game : 1
groovy:000> r = new com.pluralsight.Review([game: g, reviewDate: new Date(), reviewText: 'Review 1'])
===> com.pluralsight.Review : (unsaved)
groovy:000> r.save()
===> com.pluralsight.Review : 2
groovy:000> r2 = new com.pluralsight.Review([game: g, reviewDate: new Date(), reviewText: 'Review 2'])
===> com.pluralsight.Review : (unsaved)
groovy:000> r2.save()
===> com.pluralsight.Review : 3
groovy:000> retrieve = com.pluralsight.Game.get(1)
===> com.pluralsight.Game : 1
groovy:000> retrieve.reviews
===> null
最佳答案
您为什么期望数据库更改?
重要的是要在谈论双向关系时了解Grails / GORM(和Hibernate)的含义。
当关系的两端都引用另一侧时,该关系称为双向。因此,Game
具有Reviews
的集合,并且Review
具有对Game
的引用。这只是在谈论应用程序代码。数据库只需要1个外键即可完全存储关系。
您也可以使用联接表,但是在这种情况下(我认为),这确实过高了。但是Grails / GORM允许这样做,只需在docs的static mapping
部分中查看如何映射它即可。
关于grails - 与Grails hasMany和belongsTo创建双向关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532836/
class Owner { static hasMany = Dog } class Sitter { static hasMany = Dog } class Dog { s
如果我有以下型号: module.exports = function (sequelize, DataTypes) { var WorkingCalendar = sequelize.defin
我有这样的问题 - 有 3 个表,例如 Clients、Hats、Tshirts。每个客户都有一顶帽子和一件 T 恤关系。我是这样做的: ` ClientsModel: var $name
我使用 Laravel 5.6,我有 3 个模型: 地区 商店 商家 区域有很多商店: public function stores() { return $this->hasMany('Be
我已经在几个地方看到过要“远离”这一点,但是唉 - 这就是我的数据库的构建方式: class Album extends Eloquent { // default connection
如何制作域类以在 Grails 中创建“共享”数据但不“属于”另一个数据的表?例如,我想制作 2 张 table : 具有 2 个主要列的人员表 名称 - 技能 拉里 - 跳跃,大笑,拳击 curl
当子类可能属于父类中的两个属性之一(但不能同时属于两者)时,我无法理解 Grails 中的 belongsTo - hasMany 关系的概念。 例如: class Store { Integ
我创建了Child域,并为其添加了2个父级。我声明为一位 parent 。我在belongsTo约束的帮助下声明了另一个 parent 。 打包多个 parent class Child {
场景一: 我有两个类: 发表 : class Post { String content Date dateCreated static constraints = { content(blank:
我以为答案是很基本的,但是我对Grails还是很陌生,并且在其他任何地方都很难找到我的问题的答案。 基本上,我有两个域类,用户和事件。事件与用户具有“belongsTo”关系,而用户与事件具有“has
要在 Grails 中创建一对一关系,我可以执行以下操作: class Person { static hasOne = [address: Address] } 在这种情况下,地址表具有其人
有人告诉我,要完成将 $belongsTo 映射到非主键,我会将外键设置为 false 并由另一个论坛上的某人(实际上是 IRC)设置条件。但是,我不认为我这样做是正确的。下面是我正在尝试的 $bel
所以我有这个模型,“票”,其中包含(除其他外)“category_id”列。 为简化起见,以下是我的数据的外观: 门票表 ID 标题 类别编号 1 问题 #1 2 2 问题#2 4 3 问题 #3 1
class Batch extends Eloquent { public function coupons() { return $this->hasMany('Coupon
谁能向我解释在ManyToMany关系中belongsTo的作用是什么?对于OneToOne关系,这很明显:如果删除了“OneToMany”侧(主表)中的记录,则相应的“ManyToOne”(子表)也
我有一些模型正在尝试关联。 一种模型是 Item,一种是 Slide,另一种是 Asset。 项目下方有多个幻灯片。 Assets 基本上是已上传的文件(图像、mp3 等),幻灯片是显示 Assets
我正在尝试保存属于另一个模型的记录。然而,我得到一个错误,指出外键丢失。 CakePHP 文档显示了此工作的示例,但我无法让它工作。 这是表格: echo $this->Form->creat
好的必填信息: DEBUG: ------------------------------- DEBUG: Ember : 1.3.1 DEBUG: Ember Data : 1.0.0-b
我有一个顾问表,它有一个链接到“specilaties”表的外键“specialty_id”。 class Consultant extends AppModel { public $belo
我正在处理 belongsTo 关系,但需要知道如何指定要关联的字段。 我有一个报告和一个汽车表。 汽车表以 ID 字段作为 UI。当对汽车进行报告时,我希望从列表中删除该汽车,以便我使用示波器。 在
我是一名优秀的程序员,十分优秀!