OK, new day, new perspectives. This is the best way to do the mapping / type:

class CastToIntegerType(TypeDecorator):
    impl = String

    def column_expression(self, col):
        return cast(col, Integer)

    def process_bind_param(self, value, dialect):
        return str(value)


class Person(Base):
    __tablename__ = 'person'
    id = Column('id_string', CastToIntegerType, primary_key=True)

    pets = relationship(
        'Pets',
        primaryjoin='foreign(Pets.person_id)==cast(Person.id, Integer)')



we keep the Python conversion of int to string in Python, then cast in the primaryjoin. That way lazyload gives us:

SELECT pets.id AS pets_id, pets.person_id AS pets_person_id
FROM pets
WHERE pets.person_id = CAST(%(param_1)s AS INTEGER)
2015-09-16 14:37:00,449 INFO sqlalchemy.engine.base.Engine {'param_1': '5'}

joinedload gives us:

SELECT CAST(anon_1.person_id_string AS INTEGER) AS anon_1_person_id_string, pets_1.id AS pets_1_id, pets_1.person_id AS pets_1_person_id
FROM (SELECT person.id_string AS person_id_string
FROM person
LIMIT %(param_1)s) AS anon_1 LEFT OUTER JOIN pets AS pets_1 ON pets_1.person_id = CAST(anon_1.person_id_string AS INTEGER)


so no double cast, and also the fact that we have string coersion still happening in the lazyload is appropriate, as the type converters take place the same way as they would in the joined load.




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