2014-01-27 171 views
0

我在Grails中在PostgreSQL中生成我的表時遇到問題。我有一個簡單的電子郵件和EmailAttachment域類與hasManybelongsTo的關係。這種設置在我們的生產服務器(AS400 DB2)上運行良好,但是當我嘗試在PostgreSQL上運行我的程序(新的開發環境)時,Email類沒有attachment_id列。Grails在PostgreSQL中不創建外鍵列

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 
} 
} 

而且,這裏有相關線路從架構出口:

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']無濟於事,也不願意,並嘗試其他收集類型附件。預先感謝您的時間和大腦細胞。

回答

0

該電子郵件沒有attachment_id列,因爲它是一對多的一方。在這種情況下,許多方面都附帶在email_id int4 not null專欄中對其擁有的電子郵件的引用。給定一個ID爲42的電子郵件,您(和Grails/GORM/Hibernate)可以通過查詢email_id = 42的表中的所有行來查找所有附件。

+0

感謝您的回覆;當我使用DB2時,Grails創建了一個Attachment_ID列,所以我在代碼中的其他地方引用了它。我只是刪除這些,因爲他們顯然不需要在那裏。 – user3006324