On 06/19/2014 10:24 AM, Mike Bayer wrote:
On 6/19/14, 1:05 PM, AM wrote:
What I am storing is things like string versions of lists, tuples and
dicts, for e.g.:

str([1, 2, 3])
str({'a':1}

and so on. ast.literal_eval will only parse those and return those, it
does not evaluate expressions and statements so no real code at all.

I got around this issue by creating a PythonASTLiteralColumn based on
the example in the docs and that worked perfectly.

Having said that I would still like to understand how to approach a
situation where I want a hybrid property that is a normal python data
type, if that's possible at all?
SQLAlchemy is a bridge between Python and SQL but there is no magic in
creating the thing on each side of that bridge.   If you want a SQL
query that interrogates a column of data in a way that is meaningful
regarding some kind of in-Python behavior, you need to decide how that
behavior will be expressed in your SQL query.   The hybrid will work
fine at the instance level but if you want it to behave meaningfully in
a SQL query you'd first need to know what the SELECT statement you want
will actually look like in terms of raw SQL.


Ah ok, I see what you mean. In my particular case I really don't want anything special. Basically if I have a table like I mentioned before:


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)

The only queries I am going to be running at the SQL level are of the form:

select _ports from system where _ports is not null;
select _ports from system where class_number = 1
update system set _ports="[(1, 2, 3), ...]" where class_number = 1
inserts and deletes.

What I wanted at the python end was that if I ran this query

select _ports from system where class_number = 1

I would get
result.ports = [(1, 2, 3), ...]  # python list

instead of
result.ports = "[(1, 2, 3), ...]"  # python string

As I mentioned I can do that via the custom column type, but wanted to find out if there was an easier way that I was missing.

Thanks for all your help.

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