views:

19

answers:

1

I got a problem with PostGreSQL 8.4 and tables reflection. My metadata object seems to be ok (it has foreign keys, primary keys, every columns and tables). But when I try to associate an object to an another one through a Foreign key, I get : "sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'EventParameters' 'INSERT INTO event (".

I'm using SQLAlchemy 0.6.3 (psycopg2 2.2.1) and PostGreSQL 8.4.5

Here, my code :


#! /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()

and my database :


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) 
);

It seems that SQLAlchemy don't recognize the attribute "eventparametersid" as a relationship ...

Thank you in advance Fabien

A: 

To make your code working you should change the mapper invocation for the Event class to include the mapping between the two classes

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

then use that column to assign the EventParameters instance

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

e = Event()
e.publicid = 'test'
e.eventparameters = ep
mnencia