New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lost Json Update immediately after object creation #283

Closed
jamesstidard opened this Issue Aug 11, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@jamesstidard

jamesstidard commented Aug 11, 2017

I noticed I was loosing some updates during writing to a Json attribute. I guess the Json attribute doesn't get marked as dirty when modifying.

Here's an example ('foo' is lost):

from pony.orm import *

db = Database()
db.bind('sqlite', filename=':memory:', create_db=True)


class Person(db.Entity):
    name = Required(str)
    body = Optional(Json, default={})


db.generate_mapping(create_tables=True)


with db_session:
    p = Person(name='Bort', body={'qux': 'baz'})
    flush()
    pid = p.id
    show(p)
    p.name = 'terry'
    p.body['foo'] = 'bar'
    commit()
    show(p)

with db_session:
    p = Person[pid]
    show(p)

@kozlovsky kozlovsky closed this in b73d6b0 Aug 24, 2017

@kozlovsky kozlovsky added this to the 0.7.3 milestone Aug 24, 2017

@kozlovsky kozlovsky self-assigned this Aug 24, 2017

@kozlovsky kozlovsky added the bug label Aug 24, 2017

@kozlovsky

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Aug 24, 2017

Member

Thanks for reporting, your example should work now.

But the following examples will not work:

data = {'age': 20}
p = Person(name='John')
p.body = data
data['tel'] = '123-45-67'

items = ['foo', bar]
p = Person(name='Mike')
p.body = items
items.append('baz')

In these examples, Json field will not get changes made after the attribute assignment, because the value of attribute is copied on assignment now. The correct code looks like this:

data = {'age': 20}
p = Person(name='John')
p.body = data
p.body['tel'] = '123-45-67'

items = ['foo', bar]
p = Person(name='Mike')
p.body = items
p.body.append('baz')
Member

kozlovsky commented Aug 24, 2017

Thanks for reporting, your example should work now.

But the following examples will not work:

data = {'age': 20}
p = Person(name='John')
p.body = data
data['tel'] = '123-45-67'

items = ['foo', bar]
p = Person(name='Mike')
p.body = items
items.append('baz')

In these examples, Json field will not get changes made after the attribute assignment, because the value of attribute is copied on assignment now. The correct code looks like this:

data = {'age': 20}
p = Person(name='John')
p.body = data
p.body['tel'] = '123-45-67'

items = ['foo', bar]
p = Person(name='Mike')
p.body = items
p.body.append('baz')
@jamesstidard

This comment has been minimized.

Show comment
Hide comment
@jamesstidard

jamesstidard Aug 24, 2017

@kozlovsky Thanks a lot for taking a look and fixing this. This works just on the top-level object, right?

For example this wouldn't work either, right?

data = {'contact_details': {}}
p = Person(name='John')
p.body = data
p.body['contact_details']['tel'] = '123-45-67'

jamesstidard commented Aug 24, 2017

@kozlovsky Thanks a lot for taking a look and fixing this. This works just on the top-level object, right?

For example this wouldn't work either, right?

data = {'contact_details': {}}
p = Person(name='John')
p.body = data
p.body['contact_details']['tel'] = '123-45-67'

kozlovsky added a commit that referenced this issue Aug 24, 2017

@kozlovsky

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Aug 24, 2017

Member

Now it should work for arbitrary nested objects too =)

Member

kozlovsky commented Aug 24, 2017

Now it should work for arbitrary nested objects too =)

@jamesstidard

This comment has been minimized.

Show comment
Hide comment
@jamesstidard

jamesstidard Aug 24, 2017

@kozlovsky Very nice. Thanks again.

jamesstidard commented Aug 24, 2017

@kozlovsky Very nice. Thanks again.

@kozlovsky kozlovsky changed the title from Lost Json Update to Lost Json Update immediately after object creation Oct 23, 2017

kozlovsky added a commit that referenced this issue Oct 23, 2017

Pony ORM Release 0.7.3 (2017-10-23)
# New features

* `where()` method added to query
* `coalesce()` function added
* `between(x, a, b)` function added
* #295: Add `_table_options_` for entity class to specify engine, tablespace, etc.
* Make debug flag thread-local
* `sql_debugging` context manager added
* `sql_debug` and show_values arguments to db_session added
* `set_sql_debug` function added as alias to (to be deprecated) `sql_debug` function
* Allow `db_session` to accept `ddl` parameter when used as context manager
* Add `optimistic=True` option to db_session
* Skip optimistic checks for queries in `db_session` with `serializable=True`
* `fk_name` option added for attributes in order to specify foreign key name
* #280: Now it's possible to specify `timeout` option, as well as pass other keyword arguments for `sqlite3.connect` function
* Add support of explicit casting to int in queries using `int()` function
* Added modulo division % native support in queries

# Bugfixes

* Fix bugs with composite table names
* Fix invalid foreign key & index names for tables which names include schema name
* For queries like `select(x for x in MyObject if not x.description)` add "OR x.info IS NULL" for nullable string columns
* Add optimistic checking for `delete()` method
* Show updated attributes when `OptimisticCheckError` is being raised
* Fix incorrect aliases in nested queries
* Correctly pass exception from user-defined functions in SQLite
* More clear error messages for `UnrepeatableReadError`
* Fix `db_session(strict=True)` which was broken in 2d3afb2
* Fixes #170: Problem with a primary key column used as a part of another key
* Fixes #223: incorrect result of `getattr(entity, attrname)` when the same lambda applies to different entities
* Fixes #266: Add handler to `"pony.orm"` logger does not work
* Fixes #278: Cascade delete error: FOREIGN KEY constraint failed, with complex entity relationships
* Fixes #283: Lost Json update immediately after object creation
* Fixes #284: `query.order_by()` orders Json numbers like strings
* Fixes #288: Expression text parsing issue in Python 3
* Fixes #293: translation of if-expressions in expression
* Fixes #294: Real stack traces swallowed within IPython shell
* `Collection.count()` method should check if session is alive
* Set `obj._session_cache_` to None after exiting from db session for better garbage collection
* Unload collections which are not fully loaded after exiting from db session for better garbage collection
* Raise on unknown options for attributes that are part of relationship
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment