Hi.

I am having some trouble understanding how to use native python data types with hybrid properties. I have the following model. I am using flask-sqlalchemy however I run into the same issue in straight sqlalchemy too.

class SystemModel(BaseModel):
    __tablename__ = 'system'

    class_number = DB.Column(DB.Integer, DB.ForeignKey(
        ClassModel.get_fk('number')), primary_key=True)
    name = DB.Column(DB.String, nullable=False)
    _ports = DB.Column('ports', DB.String)

    # ports is stringified [(port, proto, mapped_port), ... ]
    @hybrid_property
    def ports(self):
        if self._ports:
            return ast.literal_eval(self._ports)      <-- problem here
        return None                                   <-- also here

    @ports.setter
    def ports(self, value):
        self._ports = str(value)

I run into trouble when using this in a query:
results = SystemModel.query.with_entities(SystemModel.ports).filter(
                            SystemModel.ports != None).all()

So the problem of course is that self._ports is not really a string so literal_eval fails. I think I can use sqlalchemy.literal here but am not sure if that will help in this case or how exactly to use it.

I realize that the right way would be to have a separate ports table with the system pk being the fk+pk and then using it and I will probably go down that road, however I would still like to understand what exactly is going on here.

I did read: http://techspot.zzzeek.org/2011/10/21/hybrids-and-value-agnostic-types/ and can certainly go down that road too, however it seems a bit of overkill for what I really want.

Any help would be greatly appreciated.

Thanks
AM

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