On 5/8/15 5:09 PM, Carlus wrote:
Yes exactly, imagine and hotel vanish, and it became into an apartment, i know is a weird situation but the code is just an example.
OK so the ORM doesn't support an object changing into a new class in place. You have to emit the UPDATE that you want, and since this is joined inheritance, it means you have to emit the UPDATE, DELETE, and INSERT across all three tables manually, using Core with Session.execute(). Then you need to entirely re-load the object and whatever else has it in a collection. When the ORM loads it fresh, then it will be loaded as the new class.




example, we have these records:

Person table:
id = 1, person_name = "john", accommodation_id =1

accommodation table:
accommodation_id =1
accommodation_type = "hotel"
address = "street xxx"
hotel_name = "Hotel xxx"

So the idea is to get convert in:

accommodation_id =1 #dont need to be the same, but need to keep the same relationship with Person table
accommodation_type = "apartment"
address = "street xxx"                                #keep the same
apartm_name = "Apartment xxx" #delete fields from Hotel table and add from Apartment table

I hope now is a little more clear the example, but I said is just an educative code :)


El viernes, 8 de mayo de 2015, 21:30:45 (UTC+2), Michael Bayer escribió:



    On 5/8/15 2:41 PM, Carlus wrote:
    Hello,
    Sorry I couldnt find info about in google neither in the
    documentation,
    I have a model structure like this one:

    class Person(Base):
         __tablename__ ='person' id = Column(Integer, primary_key=True, 
nullable=False)
         person_name = Column(String, nullable=False)

         accommodation_id = Column(Integer, ForeignKey("accommodation.id 
<http://accommodation.id>"))
         accommodation = relationship("Accommodation", backref="people")

         def __init__(self, person_name):
             self.person_name = person_name


    class Accommodation(Base):
         __tablename__ ='accommodation' __mapper_args__ = 
{'polymorphic_identity':'accommodation', 
'polymorphic_on':'accommodation_type'}id = Column(Integer, primary_key=True)
         accommodation_type = Column(String(32))
         address = Column(String)


    class Hotel(Accommodation):
         __tablename__ ='hotel' __mapper_args__ = {'polymorphic_identity':'hotel' }id 
= Column(Integer, ForeignKey('accommodation.id <http://accommodation.id>'), 
primary_key=True)
         hotel_name = Column(String)

         def __init__(self, name, address):
             self.hotel_name = name
             self.address = address

    class Apartment(Accommodation):
         __tablename__ ='apartment' __mapper_args__ = 
{'polymorphic_identity':'apartment' }id = Column(Integer, 
ForeignKey('accommodation.id <http://accommodation.id>'), primary_key=True)
         apartm_name = Column(String)

         def __init__(self, name, address):
             self.apartm_name = name
             self.address = address
    Where each Person is associate to some accommodation (thanks to
    polymorphic can be Hotel or Apartment), each accommodation can
    have more than one Person. Im having problems when I try to
    update some record in the table accommodation, mainly when for
    example updating the accomodation_type (an Apartment became into
    a Hotel, where would get new records in Hotel and would delete
    any info from the record in Apartment and keeping the same id,
    address, and relationship with People.
    I'm not sure what angle you are coming from.  Did you want to emit
    an UPDATE for accomodation_type?  That would be awkward; it means
    a Hotel vanishes and becomes an Apartment. Do you mean, you just
    want to take a Person, associated with some Hotel, and move that
    Person to an Apartment?  That would just be an UPDATE of
    Person.accomodation_id.   Just change the Person.accommodation
attribute to the parent record you want. Not sure what you mean. You'd need to illustrate what rows you are looking to modify for
    me to understand which one you are getting at.
    -- 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+...@googlegroups.com <javascript:>. To post to this
    group, send email to sqlal...@googlegroups.com <javascript:>.
    Visit this group at http://groups.google.com/group/sqlalchemy
    <http://groups.google.com/group/sqlalchemy>. For more options,
    visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.

-- 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 <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.

--
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to