SQL Alchemy相关对象错误

时间:2010-04-14 13:29:44

标签: python sqlalchemy

from sqlalchemy.orm import relation, backref
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, Date, Sequence
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class GUI_SCENARIO(Base):
    __tablename__ = 'GUI_SCENARIO'
    Scenario_ID = Column(Integer, primary_key=True)
    Definition_Date = Column(Date)

    guiScenarioDefinition = relation('GUI_SCENARIO_DEFINITION',
        order_by='GUI_SCENARIO_DEFINITION.Scenario_Definition_ID', 
        backref='guiScenario')

    def __init__(self, Scenario_ID=None, Definition_Date=None):
        self.Scenario_ID = Scenario_ID
        self.Definition_Date = Definition_Date

class GUI_SCENARIO_DEFINITION(Base):
    __tablename__='GUI_SCENARIO_DEFINITION'
    Scenario_Definition_ID = Column(Integer, Sequence('Scenario_Definition_ID_SEQ'), primary_key=True)
    Scenario_FK = Column(Integer, ForeignKey('GUI_SCENARIO.Scenario_ID'))
    Definition_Date=Column(Date)
    guiScenario = relation(GUI_SCENARIO, backref=backref('guiScenarioDefinition', order_by=Scenario_Definition_ID))
    def __init__(self, Scenario_FK, Definition_Date):
        self.Scenario_FK = Scenario_FK
        self.Definition_Date = Definition_Date

    guiScenario = relation(GUI_SCENARIO, 
        backref=backref('guiScenarioDefinition', 
            order_by=Scenario_Definition_ID))


        tableNameScenario = "GUI_SCENARIO"
        scenarioClass = getattr(MappingTablesScenario, tableNameScenario)
        tableScenario = Table(tableNameScenario, meta, autoload=True)
        mapper(scenarioClass, tableScenario)


        scenarioName = scenarioDefinition.name
        scenarioDefinitionDate = datetime.today()
        newScenario = MappingTablesScenario.GUI_SCENARIO(scenarioName, 
                                                         scenarioDefinitionDate)
        print newScenario.guiScenarioDefinition

If I try to get the objects related to a scenarioObject, I always get this error:

有谁知道,为什么我会收到此错误?

我正在使用SQLAlchemy 0.5.8。

1 个答案:

答案 0 :(得分:0)

我相信你不应该致电mapper()。当您使用declarative_base时,您可以用速记样式一次定义表,类和映射。

我建议你删除对mapper()的调用。