> On Dec 14, 2014, at 4:58 AM, Sumin Byeon <[email protected]> wrote:
> 
> Hola, everyone!
> 
> This is my first time posting on this mailing list, so please be 
> understanding if I failed to stick with rules. If someone could kindly point 
> out what I did wrong, I will try to address it at my best effort.

no worries at all!   we have basically two kinds of list posters, those who are 
concerned about this and do a great job, and then…people who I just don’t know 
what their problem is ;).


> 
> Let me briefly describe what I'm trying to do with SQLAlchemy. I'm building a 
> web application with Flask (http://flask.pocoo.org) where it uses two 
> different database backend systems. It uses SQLite when deployed locally, 
> while using PostgreSQL when deployed on a server.
> 
> My trick was working good until I updated SQLAlchemy from 0.9.7 to 0.9.8. I 
> was doing something like this:
> 
> from sqlalchemy.dialects.postgresql import JSON
> 
> if db.engine.driver != 'psycopg2': 
>     JSON = db.String 
> 
> So it actually uses a JSON-type field with PostgreSQL but uses a string field 
> with SQLite. After upgrading to SQLAlchemy 0.9.8, I've been getting the 
> following error message:

OK that’s not a great way to do that.  I don’t know offhand what change in 
0.9.8 may have caused this to change behavior but it doesn’t matter much. 

the first thing that I don’t understand is, if you use the String type you’re 
going to get a raw JSON string back, like “{‘foo’: ‘bar’}”, not a data 
structure as you would in Postgresql.  So how is your program accommodating 
having two different return types like that?

if you want to have a datatype switch in this way, there is a utility to make 
it easy known as a variant, so if you did want String or JSON, you’d do this:

my_json_type = String().with_variant(postgresql.JSON(), ‘postgresql’)

then “my_json_type” is your datatype that you use in your tables.  So that’s 
the import switch solved.

Then as far as that a String doesn’t do any JSON conversion, you probably want 
to use the example JSON type at 
http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#marshal-json-strings 
<http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#marshal-json-strings>.  
Again, you can call with_variant() on this to get Postgresql’s fancier JSON() 
type on the PG backend.


> 
> File 
> "....lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.py", 
> line 220, in result_processor 
>  json_deserializer = dialect._json_deserializer or json.loads 
> AttributeError: 'SQLiteDialect_pysqlite' object has no attribute 
> ‘_json_deserializer'

so a thing to note is, if you’re talking to a SQLite database, at no point 
should anything in dialects/postgresql be running at all.   None of the types 
in postgresql etc. may be used unless you’re actually talking to a postgresql 
database.   


> 
> At json.py:220,
> 
> json_deserializer = dialect._json_deserializer or json.loads
> 
> Shouldn't it be something like this?
> 
> json_deserializer = dialect._json_deserializer if hasattr(dialect, 
> '_json_deserializer') else json.loads

no, because json.py only works with Postgresql dialects, which definitely have 
a _json_deserializer attribute ;)





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

Reply via email to