2010-10-28 35 views
1

我遇到了PostGreSQL 8.4和表反射的問題。我的元數據對象似乎沒問題(它有外鍵,主鍵,每個列和表)。但是,當我嘗試通過外鍵將對象關聯到另一個對象時,我得到:「sqlalchemy.exc.ProgrammingError:(ProgrammingError)無法調整類型'EventParameters''INSERT INTO event(」。使用SQLAlchemy/PostGreSQL的「外部鍵屬性無法修改類型」錯誤

I 「M使用的SQLAlchemy 0.6.3(psycopg2 2.2.1)和PostgreSQL 8.4.5

在這裏,我的代碼:


#! /usr/bin/env python 

from sqlalchemy import MetaData, create_engine 
from sqlalchemy.orm import mapper, sessionmaker 

class EventParameters(object): 
    pass 

class Event(object): 
    pass 

engine = create_engine('postgresql://postgres:[email protected]:5432/renass') 
metadata = MetaData() 
metadata.reflect(bind=engine) 
Session = sessionmaker(bind=engine) 
session = Session() 
mapper(EventParameters, metadata.tables['eventparameters']) 
mapper(Event, metadata.tables['event']) 

ep = EventParameters() 
ep.publicid = 'test' 

e = Event() 
e.publicid = 'test' 
e.eventparametersid=ep 

session.add(e) 
session.commit() 

和我的數據庫:


CREATE TABLE EventParameters ( 
    id SERIAL PRIMARY KEY, 
    publicID varchar(255) UNIQUE NOT NULL, 
    description varchar(255), 
    creationInfo_agencyID varchar(64), 
    creationInfo_agencyURI varchar(255), 
    creationInfo_author varchar(128), 
    creationInfo_authorURI varchar(255), 
    creationInfo_creationTime time, 
    creationInfo_version varchar(64) 
); 

CREATE TABLE Event ( 
    id SERIAL PRIMARY KEY, 
    eventParametersID integer NOT NULL REFERENCES EventParameters(id), 
    publicID varchar(255) UNIQUE NOT NULL, 
    preferredOriginID integer, 
    preferredMagnitudeID integer, 
    preferredFocalMechanismID integer, 
    type EventType, 
    typeCertainty EventTypeCertainty, 
    creationInfo_agencyID varchar(64), 
    creationInfo_agencyURI varchar(255), 
    creationInfo_author varchar(128), 
    creationInfo_authorURI varchar(255), 
    creationInfo_creationTime time, 
    creationInfo_version varchar(64) 
); 

看來,SQLAlchemy的唐't r ecognize屬性「eventparametersid」的關係......

預先感謝您 法比安斯基

回答

1

爲了讓你的代碼工作,你應該改變的事件類映射器調用,包括兩個類

之間的映射
mapper(Event, metadata.tables['event'], properties={ 
    'eventparameters': relation(EventParameters) 
}) 

然後使用該列分配EventParameters實例

ep = EventParameters() 
ep.publicid = 'test' 

e = Event() 
e.publicid = 'test' 
e.eventparameters = ep 
+0

謝謝你,我沒有讀夠DOCUME ntation ... :) – 2010-11-04 20:47:23

相關問題