I just started using SQLAlchemy a few days ago, and now I have a problem that I hope someone can shed some light before I lose all my hair.
When I run unittest, see the snippet below, only the first test in the sequence is executed. The testPhysicalPrint test works very well, but testRecordingItem does not work with the NoResultFound exception. No rows were found for one (). But if I remove testPhysicalPrint from the test class, then testRecordingItem will work.
I suppose the problem has something to do with the session, but I cannot understand it.
In case someone wonders, the setup is as follows:
- Python 3.1 (Ubuntu 10.04 package)
- SQLAlchemy 0.7.2 (easy_install: ed)
- PostgreSQL 8.4.8 (Ubuntu 10.04 package)
- PsycoPG2 2.4.2 (easy_installed: ed)
Verification Example:
class TestSchema(unittest.TestCase): test_items = [
UPDATE
Here is a snippet of working code.
# -*- coding: utf-8 -*- import time import unittest from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import * Base = declarative_base() engine = create_engine('sqlite:///testdb', echo=False) DBSession = sessionmaker(bind=engine) class ItemMixin(object): """ Commons attributes for items, ie books, DVD:s... """ __tablename__ = 'testitems' __table_args__ = {'extend_existing':True} id = Column(Integer, autoincrement=True, primary_key=True) key = Column(Unicode(16), unique=True, nullable=False) title = Column(UnicodeText, default=None) item_type = Column(Unicode(20), default=None) __mapper_args__ = {'polymorphic_on': item_type} def __init__(self, key, title=None): self.key = key self.title = title class FooItem(Base, ItemMixin): foo = Column(UnicodeText, default=None) __mapper_args__ = {'polymorphic_identity':'foo'} def __init__(self, foo=None, **kwargs): ItemMixin.__init__(self, **kwargs) self.foo = foo class BarItem(Base, ItemMixin): bar = Column(UnicodeText, default=None) __mapper_args__ = {'polymorphic_identity':'bar'} def __init__(self, bar=None, **kwargs): ItemMixin.__init__(self, **kwargs) self.bar = bar