I'm trying to include a JSON field in a table with mysql 5.7.12 and 
sqlalchemy 1.1b2. I've successfully inserted JSON into the database, but 
when I load the record containing the JSON, I get the TypeError shown in 
the subject.

The database was created with charset utf8, and the connect string 
specifies utf8mb4, and I'm using pymsql to connect.

The relevant data model code:

class Person(Base):
    __tablename__ = 'Person'
    __table_args__ = {'mysql_charset':'utf8'}
    id = Column(Integer, primary_key=True)
    other_data = Column(JSON)

    def get(self, key):
        if (self.other_data and self.other_data[key]):
            return self.other_data[key]
        else:
            return undefined

    def set(self, key,value):
        if (self.other_data):
            self.other_data.data[key] = value
        else:
            self.other_data = {}
            self.other_data[key] = value


The nosetest that is raising the error:

def test_GetPersonExtraValue():
    session = DBSession()
    logger.debug("About to query for p1")
    p1 = session.query(Person).get(1)
    logger.debug("p1.first_name = " + p1.first_name)
    assert p1.get('favorite_color') == 'red'


def setup_data():
    session = DBSession()
    p1 = Person(id=1, first_name='Eric', last_name='Wittle',
                full_name='Eric L. Wittle', email='e...@wittle.net')
    p1.set('favorite_color','red')
    session.add(p1)
    session.commit()


The state of the Person table after the nosetest 
mysql> select id, other_data from Person;

mysql> select id, other_data from Person;

+----+---------------------------+

| id | other_data                |

+----+---------------------------+

|  1 | {"favorite_color": "red"} |

+----+---------------------------+

1 row in set (0.00 sec)

And finally the stack trace from the test with the error:

======================================================================
ERROR: server.tests.test_data_model.test_GetPersonExtraValue
----------------------------------------------------------------------
Traceback (most recent call last):
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/nose/case.py",
 
line 198, in runTest
    self.test(*self.arg)
  File 
"/Users/eric/Documents/Projects/ourlifestories/server/tests/test_data_model.py",
 
line 37, in test_GetPersonExtraValue
    p1 = session.query(Person).get(1)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/query.py",
 
line 829, in get
    return self._get_impl(ident, loading.load_on_ident)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/query.py",
 
line 862, in _get_impl
    return fallback_fn(self, key)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 223, in load_on_ident
    return q.one()
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/query.py",
 
line 2736, in one
    ret = self.one_or_none()
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/query.py",
 
line 2706, in one_or_none
    ret = list(self)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 90, in instances
    util.raise_from_cause(err)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/util/compat.py",
 
line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/util/compat.py",
 
line 186, in reraise
    raise value
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 75, in instances
    rows = [proc(row) for row in fetch]
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 75, in <listcomp>
    rows = [proc(row) for row in fetch]
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 435, in _instance
    loaded_instance, populate_existing, populators)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/orm/loading.py",
 
line 496, in _populate_full
    dict_[key] = getter(row)
  File 
"/Users/eric/Documents/Projects/ourlifestories/lib/python3.5/site-packages/sqlalchemy/dialects/mysql/json.py",
 
line 65, in process
    return json_deserializer(value)
  File 
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py",
 
line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

Any help on what is wrong in my config or code would be helpful.





-- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to