2011-08-24 13 views
8

के साथ पोस्टग्रेस विरासत IQQlchemy का उपयोग करके PostgreSQL तालिका विरासत बनाने के बारे में एक प्रश्न है।एसक्यूएलकेमी

मैं इस दो तालिकाओं है:

from sqlalchemy import * 
from sqlalchemy.orm import Session 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy import event 

Base = declarative_base() 

class His(Base): 
    __tablename__ = 'his' 

    idg = Column(Integer()) 
    idfk = Column(Integer()) 
    idh = Column(Integer(), Sequence('his_seq', start=1, increment=1), primary_key=True) 
    type= Column(String()) 

    __mapper_args__ = {'polymorphic_on': type} 
    __table_args__ = {'implicit_returning':False} 

    def __init__(self, idg, idfk, type): 
     self.idg = idg 
     self.idfk = idfk 
     self.type = type 

class Data(His): 
    __tablename__ = None 
# __mapper_args__ = {'polymorphic_identity': 'data', 'concrete':True} 
    __mapper_args__ = {'polymorphic_identity': 'data'} 
    text = Column(String()) 

    def __init__(self, text): 
     self.text = text 

@event.listens_for(His.__table__, 'after_create') 
def create_child_tables(target, connection, **kw): 
    connection.execute(""" 
     CREATE TABLE data(
     ) INHERITS (his) 
    """) 

    connection.execute(""" 
     CREATE OR REPLACE FUNCTION his_insert_trigger() 
     RETURNS TRIGGER AS $$ 
     BEGIN 
      IF (NEW.type='data') THEN 
       INSERT INTO data VALUES (NEW.*); 
      ELSE 
       RAISE EXCEPTION 'Table type is unknown for historical porpurses.'; 
      END IF; 
     RETURN NULL; 
     END; 
     $$ 
     LANGUAGE plpgsql;  
    """) 

    connection.execute(""" 
     CREATE TRIGGER his_insert 
     BEFORE INSERT ON his 
     FOR EACH ROW EXECUTE PROCEDURE his_insert_trigger(); 
    """) 

@event.listens_for(His.__table__, "before_drop") 
def create_child_tables(target, connection, **kw): 
    connection.execute("drop table data") 
    connection.execute("drop table his") 
    connection.execute("drop sequence his_seq") 

e = create_engine('postgresql://localhost:5433/des', echo=True) 
#Base.metadata.drop_all(e) 
Base.metadata.create_all(e) 
s = Session(e) 

s.add_all([ 
    Data('hola'), 
    Data('pedorrete'), 
    Data('pedorrete2') 
]) 

s.commit() 
s.close() 

ठीक है, इस उदाहरण (जैसे http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PostgreSQLInheritance में समझाया गया है) दो टेबल बना:

CREATE TABLE his 
(
    idg integer, 
    idfk integer, 
    idh integer NOT NULL defautl nextval('his_seq'), 
    "type" character varying, 
    CONSTRAINT __his_pkey PRIMARY KEY (idh) 
); 
CREATE TABLE data 
(
    "text" character varying, 
) 
INHERITS (his); 

से पहले किसी भी DDL आदेश पर अमल, मैं इस अजगर कोड बना दिया है , लेकिन sqlalchemy हमेशा डेटा रिकॉर्ड डालने के लिए अपनी तालिका का उपयोग करते हैं, और इन्हें डेटा और उसके पर डाला जाता है। टेक्स्ट फ़ील्ड (डेटा पर) वास्तव में उसकी मेज पर बनाई गई है।

तो, क्या एसक्यूएलकेमी को निर्दिष्ट करने का कोई तरीका है कि डेटा तालिका को उसके द्वारा विरासत में विरासत (विरासत विरासत) प्राप्त होनी चाहिए, और इसमें टेक्स्ट फ़ील्ड जोड़ना होगा, और डेटा का उपयोग करना चाहिए, न कि जब मैं डेटा पर कोई रिकॉर्ड डालता हूं?

सम्मान।

उत्तर

1

SQLAlchemy जितना संभव हो पोर्टेबल होने की कोशिश करता है, इसलिए यह कई पोस्टग्रेस-विशिष्ट विशेषताओं का समर्थन नहीं करता है। एसए लड़के से इसी तरह की समस्या वाले किसी व्यक्ति को यहां दिया गया उत्तर दिया गया है: http://www.mail-archive.com/[email protected]/msg17443.html

संबंधित मुद्दे