that's actually a serious issue, which I have just repaired in
https://bitbucket.org/zzzeek/sqlalchemy/issue/3159/cant-insert-null-into-a-json-column-if.
If you check out the latest master or wait until 0.9.8, you will be able to
persist this using the value null(), e.g. from sqlalchemy import null, or
specify the JSON type including the new flag none_as_null:
data = Column(postgresql.JSON(none_as_null=True))
As a workaround, you probably need to use a custom type like that of
JSONEncodedDict:
http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#marshal-json-strings
from sqlalchemy.types import UserDefinedType
class PGJSON(UserDefinedType):
def get_col_spec(self):
return "JSON"
def bind_processor(self, dialect):
def process(value):
if value is None:
return None
else:
return json.dumps(value)
return process
if you're using psycopg2, the result side is coerced to JSON automatically by
psycopg2.
On Aug 7, 2014, at 2:15 AM, Priidu Kull <[email protected]> wrote:
> Hello,
>
> rows = [{"id": 1, "json_value": [{"key": "value"}, {"key2": "value2"}]},
> {"id": 2, "json_column": None}]
> insert_query = table.insert().values(rows)
> connection.execute(insert_query)
>
> Doing this will have "null" (String) entered to the row where id=2.
>
> Is there any way to properly do multiple row insert where value of some JSON
> columns is NULL?
>
> With Best Wishes,
> Priidu Kull
>
> --
> 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.
--
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.