That is not going to work.

let me explain:


Polymorphic inheritance uses tables and discriminators and some logic somewhere 
in the system. (As far as I can tell both SA and databases can do the job, i 
have to look into that)

For each object there will be a table, including the "base" object.


simple:

class x(BAse)
        Id
        discriminator

        some_column_x


class y(x)
        id ….
        some_column_y

class z(x)
        id …
        some_column_y

will result in the following table structure (assume the name of the object is 
the name of the table and discriminator:

x: 
        Id
        discriminator
        some_column_x   

y:
        Id
        some_column_y

z
        Id
        some_column_z

now if you create a record in y or z two records are created, one in y or z and 
one in x. Take note that the Id's will be the same and that the discriminator 
in x will be y or z.
the Id are a sequence of the x table.


If you however create an x record, only one record will be created.

You are planning on changing from x to y or z later in the process.

you can do so but there is little more work to do here.

what you have to compensate for is the missing record in y or z

so if you have an x object you want to convert into an y object (Or Eployee to 
Engineer)

Load the Employee Object.
create a new Engineer object
copy the "shared" values from the Employee, including Id and EXCLUDING 
discriminator!!!
now you have converted the Employee into an Engineer

to make it persistant, remeber that we miss the record in Engineer. Saving 
Engineer by Session.add(EngineerInstance) would do the trick.
You should test if it is needed to Session.delete(EmployeeInstance), and 
Session.commit() first. Session.add(Engineer) might very well overwrite but It 
might throw up a duplicate key error.

This will help you out I think.

Martijn


On Jan 18, 2012, at 19:08 , Thierry wrote:

> Hi
> 
> I'm trying to mimick the example from the documentation (employee/
> manager/engineer)
> 
> I've been able to add employees, and engineers, and all works as
> expected
> 
> now my next step would be, creating an 'Employee' object, and some
> time later, decide that he's in fact an engineer.
> 
> so my first attempt would read something like
> 
> person = session.query(Employee).filter(..)
> -> retrieve the instance
> person.type='engineer'
> session.commit()
> 
> and I would have expected at that point, when retrieving again
> person = session.query(Engineer).filter(..)
> 
> to now have an instance of Engineer
> but that's not what I am seeing, I still get an instance of Employee
> 
> I haven't found this issue addressed in the doc, sorry if I missed it
> What should be the right way to achieve this ? Am I missing something
> obvious here ?
> 
> Many thanks in advance
> 
> ---
> I can provide a complete working code of course, I just would like to
> make a sanity check before I go through the trouble of extracting the
> relevant part
> 
> 
> -- 
> 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.
> 

-- 
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