М
In the meantime, If you really need a solution right now,
I’ve made https://github.com/luckydonald/pony_up which might help you.
It is more of a manual approach of writing migrations, but executes them automatically, and thus it is still better then executing SQL queries yourself.
Permalink Bot
Carlos
Hi! I have been working recently with PonyORM and it is awesome! 🙂 I was wondering, how is the GraphQL development going? I have read some posts on Github, Stackoverflow, about plans of its integration (I think the oldest I found was of 2015), but couldn't find anything in the documentation
Alexander
Hi Carlos! We don't develop GraphQL integration right now
Alexander
Maybe we'll return to it later
Carlos
got it! Thanks, Alexander! 👍🏼
Argenis
Hi guys, just meeting pony. A question about Pony and ORM's in general. Can Pony help me to generate the query detached from the database? I mean I need to generate complex sql agg query for Apache Spark but I do not want to concatentate SQL strings. Any help_
Matthew
existing_review_data = {k: v for k, v in existing_review.to_dict().items() if (logic) }
Matthew
existing_review is a pony object
Matthew
ProgrammingError: can't adapt type 'TrackedDict'
Matthew
I am eventually using that data to construct a new pony object
Matthew
is TrackedDict the object produced by to_dict() ?
Should I explicitly call dict() on it to fix this?
Matthew
From looking at pony source, I think I should call to_dict().get_untracked() ?
Alexander
Who raises ProgrammingError?
Matthew
pony/orm/dbapiprovider.py - wrap_dbapi_exceptions
Alexander
Can you pm me full traceback?
Matthew
yep
Alexander
And what is existing_review_data?
Matthew
do you mean existing_review ?
Alexander
No, I mean existing_review_data. Like, are you trying to send this object to database somehow?
Matthew
It is a dict which gets modified then passed to the constructor of ReviewEdit which is a pony model, as **kwargs
Alexander
Does ReviewEdit have JSON attr?
Matthew
Sorry, that’s wrong, it is used to compare against another similar dict which is used as kwargs
Matthew
Matthew
product_attributes = Optional(Json)
Alexander
Hi Matthew!
TrackedDict is returned for json attrs. Its purpose is to notify object when the value of attribute (or some of its subcomponent) has changed, so object will know it should be saved to database on commit.
TrackedDict is instance of a normal dict, so if a library which try to "adapt" TrackedDict value (whatever it means) should work with TrackedDict instance without problems. But probably psycopg2 library uses a check like type(value) is dict instead of isinstance(value, dict). But I'm wondering why you are trying to pass a dict as a parameter value to SQL query, it looks strange.
Yes, you need to call get_untracked, but not on obj.to_dict(), but on obj.some_json_attr_value. Maybe you need to use some helper like
def adapt_dict(d):
result = {}
for k, v in d.items():
if isinstance(v, TrackedDict):
v = v.get_untracked()
result[k] = v
retrn result
Matthew
thank you, that makes sense, I will try it
Alexander
Maybe obj.to_dict() should always do .get_untracked() to get copy of JSON value which is ubinded from the object
Alexander
Matthew
existing_review_data = {k: v for k, v in existing_review.to_dict().items()
if k not in forbidden_keys}
existing_review_data = adapt_pony_dict(existing_review_data)
Matthew
The exception is thrown on tthe first line, before the function you provided can run
Alexander
hmm
Matthew
existing_review_data = {k: v for k, v in adapt_pony_dict(existing_review.to_dict()).items()
if k not in forbidden_keys}
Matthew
This also fails
Matthew
This line throws the exception:
Matthew
x = existing_review.to_dict()
Matthew
By the way, the exception is only being thrown in a tiny minority of cases
Matthew
The only JSON field in existing_review value is {}
Alexander
Looks like a bug in pony, I'll look into it
Matthew
When I try existing_review.to_json() I get:
Matthew
PermissionError: The current user None which belongs to groups ['anybody'] has no rights to see the object Review[15498384] on the frontend
Alexander
Don't use to_json, it was experimental undocumented method and need to be rewritten
Matthew
ah!
Alexander
What's your dbms?
Matthew
Postgres
Matthew
If in ipython I load the Review directly then call to_dict(), it works
Alexander
The problem is not in .to_dict(). .to_dict() calls flush(), and the error happens inside flush
Alexander
JSON attribute content represented in two ways. For database it is a string with serialized JSON. For Python user it is a dict. Pony automatically converts dict to serialized string when it saves objects to database. But in your case for some reason the dict was passed to database without conversion to string. I'll investigate it
Matthew
Thank you!
Alexander
It would be good if you discover reproducible example of how it happens
Matthew
It seems to only happen with a minority of jobs of that type
Matthew
but it consistently happens for at least that one object
Alexander
I suspect this object was previously pickled/unpickled
Matthew
I don’t think it has been
Alexander
Maybe you did something unusual with this object before calling to_dict
Matthew
The only thing I do is set a value on the object if thatt value is None
Matthew
if x.y is None:
x.y = rue
Matthew
and set another attribute to None
Matthew
#if existing_review.review_verified is None:
# existing_review.review_verified = review_verified
#existing_review.deleted = None
Matthew
when commented out, it works
Matthew
#existing_review.deleted = None
Matthew
this is the code that when commentted out makes it work
Alexander
I think the lines that you’ve commented out just manifests the error. They modify the object, and then to_dict internally calls flush() trying to save the modified object, and flush fails to save corrupted object which leads to exception. But object state corruption happens before the lines that you’ve commented out.
Matthew
The only line before those lines which involve that object is:
Matthew
existing_review = models.Review.get(amazon_review_id=amazon_review_id, product=product)
Matthew
Does Pony ever issue an UPDATE SQL query when doing Entity.get() ?
Matthew
I know that sounds strange, but that seems to be happening for me
Matthew
and then failing
Alexander
If you have some unsaved changes, Pony will save them before issuing SELECT to be sure that SELECT sees these changes
Matthew
Ah, so if I end up “get” ing the same object twice, and the object was modified the first time, that could explain it
Alexander
Do you observe some incorrect behavior?
Matthew
I am ultimately getting this error:
Matthew
ProgrammingError: operator does not exist: json = unknown
LINE 13: AND "facebook_tasks" = '["VIEW_MONETIZATION_INSIGHTS", "AN...
Alexander
So the right part is not casted to ::json. Ok, It seems I need to fix it
Matthew
Do you need some more context from me?
Alexander
I think no
Matthew
🙂
Matthew
So I was processing the same object twice due to a bug in my code. Now the JSON error no longer appears, but I guess it is still a bug?
Alexander
Yes, I think it is a bug and I'll fix it
dovaogedot
Can I somehow use pony with python3.8?