2014-01-27 4 views
0

Grails에서 PostgreSQL에 테이블을 생성하는 데 문제가 있습니다. 나는 hasManybelongsTo의 관계를 가진 간단한 이메일과 EmailAttachment 도메인 클래스를 가지고있다. 이 설정은 프로덕션 서버 (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 열이 없습니다. 전자 메일은 one-to-many의 한면이기 때문에 attachment_id 열이 없습니다. 이 경우 첨부 파일의 많은 부분이 email_id int4 not null 열의 소유 전자 메일에 대한 참조를 갖습니다. ID가 42 인 이메일과 Grails/GORM/Hibernate는 email_id = 42를 사용하여 테이블의 모든 행을 쿼리하여 모든 첨부 파일을 찾을 수 있습니다.

+0

답변 해 주셔서 감사합니다. Grails는 DB2를 사용할 때 Attachment_ID 열을 만들었으므로 코드에 다른 곳을 참조했습니다. 분명히 거기에있을 필요가 없기 때문에 나는 그것을 제거 할 것입니다. – user3006324

관련 문제