Grails不会在PostgreSQL中创建外键列

时间:2014-01-27 20:30:12

标签: postgresql grails gorm

我无法从Grails在PostgreSQL中生成表格。我有简单的Email和EmailAttachment域类,其hasManybelongsTo关系。这个设置在我们的生产服务器(AS400 DB2)上运行良好,但是当我尝试在PostgreSQL(新的开发环境)上运行我的程序时,Email类没有attachment_id列。

Email.groovy:

class Email {

   static hasMany = [attachments:EmailAttachment]
   Integer id
   Integer version = 0
   String subject
   String recipients
   String sender
   Date sentDate
   String plainTextMessage
   Set attachments

static mapping = {
    datasources(['DEFAULT'])
    table name:'error_email', schema: Appointment.schema
    sort sentDate:'desc'
}

static constraints = {
    subject nullable:true
    version nullable:true
    recipients nullable:true
    sender nullable:true
    sentDate nullable:true
    plainTextMessage nullable:true
    attachments nullable:true
}

def String toString(){
    return subject
}
}

EmailAttachment.groovy:

class EmailAttachment {
static belongsTo = [email:ErrorEmail]

ErrorEmail email
String filename
byte[] content

static mapping = {
    datasources(['DEFAULT'])
    table name:'error_email_attachment', schema: Appointment.schema
}
static constraints = {
    filename nullable:true
    content nullable:true
}
}

此外,以下是schema-export的相关行:

alter table program.email_attachment drop constraint FK2E592AFD1D80E229;
drop table program.email cascade;
drop table program.email_attachment cascade;
drop sequence hibernate_sequence;
create table program.email (id int4 not null, version int4, plain_text_message varchar(255), recipients varchar(255), sender varchar(255), sent_date timestamp, subject varchar(255), primary key (id));
create table program.email_attachment (id int8 not null, version int8 not null, content bytea, email_id int4 not null, filename varchar(255), primary key (id));
alter table program.email_attachment add constraint FK2E592AFD1D80E229 foreign key  (email_id) references program.error_email;
create sequence hibernate_sequence;

我已尝试指定joinTable:attachments joinTable:[name: 'email_table', column: 'attachment_id', key: 'id']无效,并将其遗漏,并尝试其他集合类型进行附件。提前感谢您的时间和脑细胞。

1 个答案:

答案 0 :(得分:0)

电子邮件没有attachment_id列,因为它是一对多的一面。在这种情况下,许多方面的附件在email_id int4 not null列中引用了其拥有的电子邮件。给定一个id为42的电子邮件,您(和Grails / GORM / Hibernate)可以通过查询该表中所有行,并使用email_id = 42来查找所有附件。