cannot use JsonColumn recipie if column has primary_key=True set

Issue #2126 resolved
Former user created an issue

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/JSONColumn

ive tried this recipie for this class

SomeClass(obj)
    channel_json_conf = sa.Column(JSONCol(), primary_key=True,
                                  nullable=False, default='')

the problem was serialisation only worked for immutable types like string, for mutable types i got following exception:

  self.session.flush()
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/session.py', line 1388 in flush
  self._flush(objects)
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/session.py', line 1469 in _flush
  flush_context.execute()
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/unitofwork.py', line 302 in execute
  rec.execute(self)
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/unitofwork.py', line 446 in execute
  uow
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/mapper.py', line 1640 in _save_obj
  instance_key in uowtransaction.session.identity_map:
File '/home/ergo/python/lib/python2.6/site-packages/sqlalchemy/orm/identity.py', line 99 in __contains__
  if dict.__contains__(self, key):
TypeError: unhashable type: 'dict'

when i remove primary_key from the column definition everything works fine for both mutable and immutable types.

Comments (1)

  1. Mike Bayer repo owner

    sorry, this is not a bug for two reasons.

    1. the primary key value of an object has to be hashable. Dictionaries aren't hashable. If you wanted to modify JSONCol to emit a frozendict structure, that would fix that problem but that's beyond the scope of the example.
    2. using a raw JSON structure, and following a textual value of arbitrary length, as a primary key is just a really bad idea.
  2. Log in to comment