I am using reflection on an existing database

When I try to use polymorphic on a single table, i get an error;

./test 
Traceback (most recent call last):
  File "Knowhow.py", line 35, in <module>
    from Schema import *
  File "/home/treeve/qta/Schema.py", line 72, in <module>
    class Invoice(Base):
  File "/usr/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", 
line 50, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", 
line 249, in _as_declarative
    "table-mapped class." % cls
sqlalchemy.exc.InvalidRequestError: Class <class 'Schema.Invoice'> does not 
have a __table__ or __tablename__ specified and does not inherit from an 
existing table-mapped class.


The original schema is:

##############
engine = create_engine(login.connection)

metadata = MetaData(bind=engine)
Base = declarative_base(metadata=metadata)
Base.metadata.bind = engine
#Base.metadata.reflect()
Base.metadata.reflect(views=True)

class X(object):
    pass
db=X()



class Doc(Base):
    __tablename__ = "doc"
    __table_args__ = {'extend_existing': True}
#    id = Column(SMALLINT,primary_key=True)
#    tdate = Column(Date)
#    net   = Column(Numeric)
#    vat   = Column(Numeric)
    type = Column(String(1))
    __mapper_args__ = {
        'polymorphic_identity':'x',
        'polymorphic_on':type
    }
db.Doc = Doc

class Expense(Base):
#    __tablename__ = "expense"
#    __table_args__ = {'extend_existing': True}
    id = Column(SMALLINT, ForeignKey('doc.id'),primary_key=True)
    org = Column(SMALLINT, ForeignKey('org.id'))
    __mapper_args__ = {
        'polymorphic_identity':'E',
#        'polymorphic_identity':'expense',
    }
    expenses = relationship("Expitem", backref="exp")
db.Expense = Expense


class Invoice(Base):
#    __tablename__ = "invoice"
#    __table_args__ = {'extend_existing': True}
    id = Column(SMALLINT, ForeignKey('doc.id'),primary_key=True)
    org = Column(SMALLINT, ForeignKey('org.id'))
    __mapper_args__ = {
        'polymorphic_identity':'I',
#        'polymorphic_identity':'invoice',
    }
    items = relationship("Item", backref="invoice")
db.Invoice = Invoice

try:
   Base.metadata.create_all()
except Exception, e:
   print 'unable to access database, check the username/password\n',e
   sys.exit()
########################



If I  activate __tablename__ and  __table_args__ for Invoice and Expense, 
sqlalchemy generates SQL which references non-existant tables.

If I manually add views for  Invoice and Expense everything works.


I am using latest rel_0_8 branch

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to