On Fri, Jun 26, 2009 at 18:12, DG<[email protected]> wrote:
>
> I'm not too sure what the proper db vocab is to describe my problem
> but I will illustrate in code.  I would like to do something like the
> below:
> ---------------------------------------------------------
> class VehicleBuilder(Entity):
>    """ person who builds vehicles """
>    name = Field(Unicode(60))
>    #vehicle_type = ???     # <-- this is what I want to know how to
> do
>
> class Vehicle(Entity):
>    using_options(inheritance='multi', polymorphic='vehicle_type')
>
> class Car(Vehicle):
>    pass
> class Van(Vehicle):
>    pass
> class SUV(Vehicle):
>    pass
> ---------------------------------------------------------
> I would like to specify that every VehicleBuilder only builds a
> certain type of vehicle (e.g. 'Car').
>>>> bob = VehicleBuilder(name='Bob', vehicle_type=Car)
> I can picture the db organization of this, and I would imagine that in
> the python mapping it should return the class of Car.  Even if it
> returned the string of the class I could work with that.
> Is this possible? I've looked through the Elixir faq and docs as well
> as through SQLAlchemy, but didn't see anything *quite* like this (but
> I could've missed it).
>
> I haven't thought it out completely, but I was also thinking something
> that might work would be to define the 'VehicleType' table/class
> manually, and add a ManyToOne column to the 'Vehicle' class, but I
> think this would create a redundant column.
>
> -----------------------------------------------------
> class VehicleType(Entity):
>    name = FieldUnicode(15)
>
> class Vehicle(Entity):
>    using_options(inheritance='multi')  # not specifying 'polymorphic'
>    vehicle_type = ManyToOne('VehicleType')
>
> class Car(Vehicle):
>    def __init__(self):
>        self.vehicle_type = 'Car'
> class Van(Vehicle):
>    pass

This is actually one of those cases where Elixir gets in your way.
I'll probably fix the issue one day but don't hold your breath. That
use case would probably work on raw SQLAlchemy (with or without
declarative) with something like (this code is untested):

class VehicleType(Base):
    __tablename__ = 'vehicule_types'
    name = Column(Unicode(15), primary_key=True))

class Vehicle(Base):
    __tablename__ = 'vehicule'
    id = Column(Integer, primary_key=True)
    discriminator = Column('vehicule_type_name', Unicode(15),
ForeignKey('vehicule_types.name'))
    __mapper_args__ = {'polymorphic_on': discriminator}
    vehicle_type = relation('VehicleType')

class Car(Vehicle):
    __tablename__ = 'car'


-- 
Gaëtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to