I have a single table polymorphism question. It looks like I can't
directly set the value of the polymorphic discriminator when creating
records; it is only set correctly when you instantiate objects of the
derived class. Is that true? Why, or what is wrong here?

Here is the scenario:

Jobs have Steps, Steps have Inputs of different types (say "SRC1" and
"SRC2")

Use declarative to set up these classes:

class Job(Base):
    __tablename__ = 'job'
    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(20))
    jobnum = Column(Integer)
    steps = relation('Step', backref=('jobs'))
    def __repr__(s):
        return '<Job> %s title=%s'%(s.id,s.title)

class Step(Base):
    __tablename__ = 'step'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(20))
    job_id = Column(Integer, ForeignKey('job.id'))
    inputs = relation('Input', backref=('step'))
    def __repr__(s):
        return '<Step> %s name=%s'%(s.id,s.name)

class Input(Base):
    __tablename__ = 'input'
    id = Column(Integer, primary_key=True, autoincrement=True)
    kind = Column(String(10))
    value = Column(String(30))
    step_id = Column(Integer, ForeignKey('step.id'))
    __mapper_args__ = {'polymorphic_on': kind}
    def __repr__(s):
        return '<Input> %s kind=%s value=%s'%(s.id,s.kind,s.value)

class SRC1(Input):
    __mapper_args__ = {'polymorphic_identity': 'SRC1'}
    src1file = Column(String(100))

class SRC2(Input):
    __mapper_args__ = {'polymorphic_identity': 'SRC2'}
    src2file = Column(String(100))


insert some data like this

j1 = Job(title='Job#201',jobnum=201)
step = Step(name='job201-step1')
j1.steps.append(step)

inp1 = SRC1(value='this one works')
step.inputs.append(inp1)

inp2 = Input(kind='SRC2',value='why is kind = NULL here')
step.inputs.append(inp2)

inp3 = Input(value='kind is null here also')
inp3.kind='SRC2'
step.inputs.append(inp3)

session.add(j1)
session.commit()


when i retrieve the data notice that the 2nd and 3rd records have no
kind value, even though I tried to explicitly set it.

for i in session.query(Input):
    print i

<Input> 1 kind=SRC1 value=this one works
<Input> 2 kind=None value=why is kind = NULL here
<Input> 3 kind=None value=kind is null here also


So what happened with inp2 and inp3 where I tried to assign a value to
"kind"?

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

Reply via email to