开发者

Stuck in SQLAlchemy and Object Rlational

I'm just working on a simple database of an address book for learning sqlalchemy. This is the code under the 'tables.py' file (that is pretty like one in the tutorial!):

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String, create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

engine = create_engine('sqlite:///phone.db', echo=True)
Base = declarative_base()

class namesT(Base):

    __tablename__ = 'Names'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    sirname = C开发者_开发知识库olumn(String)
    job = Column(String)
    work = Column(String)

    def __init__(self, namesTuple):
        self.name, self.sirname, self.job, self.work = namesTuple
        print self.name, self.sirname, self.job, self.work

    def __repr__(self):
        return '%s, %s, %s, %s' % (self.name, self.sirname, self.job, self.work)

class detailT(Base):

    __tablename__ = "Details"

    id = Column(Integer, primary_key=True)
    names_id = Column(Integer, ForeignKey('Names.id'))
    type = Column(String)
    info = Column(String)
    detail = Column(String)

    names = relationship(namesT, backref='Details', order_by=id, cascade="all, delete, delete-orphan")


    def __init__(self, detailsTuple):
        self.type, self.info, self.detail = detailsTuple
        print self.type, self.info, self.detail

    def __repr__(self):
        return "%s, %s, %s" % (self.type, self.info, self.detail)

Base.metadata.create_all(engine)

And this is the codes in the 'dbtrans.py':

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tables import engine as engine
from tables import namesT as names
from sqlalchemy.orm import sessionmaker

class transaction:
    def __init__(self):
        self.Session = sessionmaker(bind=engine)
        self.session = self.Session()

    def insert_in_names(self, namesTuple):
        print namesTuple
        ed = names(namesTuple)
        self.session.add(ed)
    def find(self):
        self.session.query(names).filter(names.name=='ed').all()

    def commitAll(self):
        self.session.commit()

if __name__ == "__main__":
    tup = ('Blah', 'Blah', 'Blah', 'Blah')
    ins = transaction()
    ins.insert_in_names(tup)
#    print ins.sessQuery()
    ins.commitAll()

I just get this error every time I run the dbtrans:

sqlalchemy.orm.exc.FlushError: Instance <namesT at 0x14538d0> is an unsaved, pending instance and is an orphan (is not attached to any parent 'detailT' instance via that classes' 'names' attribute)

where is the problem?


Please correct me if I'm wrong but the error is saying there is no detailT instance, and looking at the code I can't see anywhere where the detailT class is instantiated. It looks to me that since detailT is the parent of of nameT via the relationship you can't save nameT without its parent. I would start here.


Just removed the relation from 'detailT' class and added it to 'namesT' like this:

details = relationship("detailT", backref='namesT', order_by=id, cascade="all, delete, delete-orphan")

now it's working!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜