保存具有列重命名的Grails 3域对象时出现字段错误

时间:2017-10-25 17:19:00

标签: hibernate grails gorm

我一直在将我的社交网络www.nderground.net从Grails 2移植到Grails 3. Grails域类/ GORM从Grails 2到Grails 3的工作方式似乎有细微差别。

nderground代码使用名为“邀请”的域类来保存有关通过电子邮件发送到nderground的邀请的信息。域类及其相关的约束和映射包含在下面。

“host”字段是“托管”邀请的用户的User对象。该字段映射到静态映射中的数据库字段“handle”。

当我使用Grails 3(但不是Grails 2)保存一个Invitation对象时,我收到以下错误:

Field error in object 'nderground.Invitation' on field 'host': rejected value [InvitationUser]; codes [nderground.Invitation.host.unique.error.nderground.Invitation.host,nderground.Invitation.host.unique.error.host,nderground.Invitation.host.unique.error.nderground.User,nderground.Invitation.host.unique.error,invitation.host.unique.error.nderground.Invitation.host,invitation.host.unique.error.host,invitation.host.unique.error.nderground.User,...

似乎“主机”未映射到Postgres表中的“handle”列。

class Invitation {
public enum InvitationType {
    BY_EMAIL,         // invitation was sent by email
    PASSWORD_RESET,   // invitation was sent for a password reset
    REQUESTED_INVITE, // an invitation that is requested via the www.nderground.net/ page. May be open or accepted.
    REQUESTED_CLOSED, // the requested invitation has expired
    OPEN              // the invitation is open - a matching email address in not required
}

User host // person issuing the invitation
String inviteEmail // email address for the invitee
Date inviteDate    // date the invitation was issued
Date expireDate    // date the invitation expires 
String inviteCode  // invitation code (sent to the invitee via email)
boolean accepted   // true - the invitation was accepted, false it was not
boolean makeConnection // true - connect to the invitation host, false - don't connect
InvitationType type = InvitationType.BY_EMAIL // Invitation type

static constraints = {
    host nullable : false, unique: false
    inviteEmail email: true, blank: false, nullable: false
    inviteDate nullable: false
    expireDate nullable: false
    inviteCode blank: false, nullable: false
    type blank: false
}

static mapping = {
    host column : 'handle'
    inviteCode index : 'Invite_Code_Ix'
    inviteCode unique : 'inviteEmail' // invite code and email are unique
    accepted defaultValue: false
    makeConnection defaultValue: true
    type enumType: 'ordinal'
}

}

Postgres表如下所示。 Postgres表中的“handle”列应该映射到域类中的“host”字段(同样,这适用于Grails 2,但显然不适用于Grails 3)。

CREATE TABLE invitation
(
  id bigint NOT NULL,
  version bigint NOT NULL,
  accepted boolean NOT NULL,
  expire_date timestamp without time zone NOT NULL,
  handle bigint NOT NULL,
  invite_code character varying(255) NOT NULL,
  invite_date timestamp without time zone NOT NULL,
  invite_email character varying(255) NOT NULL,
  make_connection boolean NOT NULL DEFAULT true,
  type integer NOT NULL,
  CONSTRAINT invitation_pkey PRIMARY KEY (id),
  CONSTRAINT fk94psn3lk2xu0l5gl71ayfm4ht FOREIGN KEY (handle)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_krm2ne7yedxtul61k5wdvjaqw FOREIGN KEY (handle)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT uk26487c1809285677f20d7df5c6dc UNIQUE (invite_email, invite_code),
  CONSTRAINT uk_s57hvf05ru6s0e2hyoorefmlf UNIQUE (invite_code)
)

有关如何解决此问题的任何建议?我可以删除列名称映射,但这应该在Grails 3中工作。有什么我做错了或者这是Grails 3错误吗? (我使用的是Grails 3.3.1)。

0 个答案:

没有答案