Tôi có một câu hỏi về việc tạo bảng thừa kế PostgreSQL bằng cách sử dụng SQLAlchemy.Thừa kế Postgres với SQLAlchemy
Tôi có này hai bảng:
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);
Trước khi thực hiện bất kỳ lệnh DDL, tôi đã thực hiện mã này python:
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()
Vâng, ví dụ này (như đã giải thích trong http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PostgreSQLInheritance) tạo hai bảng , nhưng sqlalchemy luôn luôn sử dụng bảng của mình để chèn dữ liệu hồ sơ, và chúng được chèn vào dữ liệu và của mình. trường văn bản (trên dữ liệu) thực sự được tạo trên bảng của anh ấy. Vì vậy, có cách nào để chỉ định cho SQLAchemy rằng bảng dữ liệu phải thừa kế (Postgres kế thừa) từ anh ta, và phải thêm trường văn bản vào nó, và phải được sử dụng dữ liệu và không phải của mình khi tôi chèn bất kỳ bản ghi trên dữ liệu ?.
Trân trọng.