
stsouko
09.04.2017
21:09:55
Hello! DB structure:
class A(db.Entity):
a = Set('B')
class B(db.Entity):
b = Set('A')
def before_delete(self):
for x in self.b:
x.delete()
then I try to use this:
B[1].delete()
I get
OperationWithDeletedObjectError: B[1] was marked to delete
I think hooks priority should be greater.

Luckydonald
10.04.2017
14:51:38
Talking about Hooks,
Is there something to hook into create/update/delete on PonyORM side?
Context is the Pony --> Elasticsearch tool I'm writing.
Currently it would look like
from database import SomeObject, db
obj = SomeObject["id123"]
Syncer.generate_index(db, "SomeObject")
Syncer.store(obj)

Google

Luckydonald
10.04.2017
14:54:58
So the idea would be to hook into Pony somewhere, to automatically have it updated.
pony.orm.core.TransactionError: create_tables() cannot be called inside of db_session
Why that?

Alexander
10.04.2017
16:44:54
In many databases it is not possible to intermix create and insert/update commands. As create_tables command already have @db_session wrapper, it typically have a little sense to wrap it in yet another db_session

stsouko
10.04.2017
16:46:49
@akozlovsky what about before delete hook?
Or hooks don't designed for this

Alexander
10.04.2017
16:51:35
It looks like a bug, I need to reproduce it

stsouko
10.04.2017
16:52:19
I used latest pypi version

Luckydonald
10.04.2017
16:52:48
I probably something old... lemme check
Mine is 0.7

Alexander
10.04.2017
16:57:35
@nougmanoff, can you provide a full traceback?
@luckydonald, data can be modified inside the database without using Pony, in that case the hook will not work

Luckydonald
10.04.2017
16:59:48
I would be happy enough with that limitation. What do I need to do?

Alexander
10.04.2017
17:01:50
Probably just override after_update / after_delete hooks and make corresponding update to elasticsearch

Google


stsouko
10.04.2017
17:04:03
Ok.
---------------------------------------------------------------------------
OperationWithDeletedObjectError Traceback (most recent call last)
<ipython-input-40-000b29768406> in <module>()
----> 1 commit()
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in commit()
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs)
73 if tb.tb_frame.f_globals.get('__name__') == 'pony.utils' and tb.tb_frame.f_code.co_name == 'throw':
74 reraise(exc_type, exc, last_pony_tb)
---> 75 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback
76 finally:
77 del exc, tb, last_pony_tb
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs)
58 return func(*args, **kwargs)
59
---> 60 try: return func(*args, **kwargs)
61 except AssertionError: raise
62 except Exception:
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in commit()
291 try:
292 for cache in caches:
--> 293 cache.flush()
294 except:
295 rollback()
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in flush(cache)
1600 with cache.flush_disabled():
1601 for obj in cache.objects_to_save: # can grow during iteration
-> 1602 if obj is not None: obj._before_save_()
1603
1604 cache.query_results.clear()
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in _before_save_(obj)
4836 if status == 'created': obj.before_insert()
4837 elif status == 'modified': obj.before_update()
-> 4838 elif status == 'marked_to_delete': obj.before_delete()
4839 def before_insert(obj):
4840 pass
<ipython-input-23-c9e3c6eedbcc> in before_delete(self)
2 b = Set('A')
3 def before_delete(self):
----> 4 for x in self.b:
5 x.delete()
6
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in __get__(attr, obj, cls)
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs)
73 if tb.tb_frame.f_globals.get('__name__') == 'pony.utils' and tb.tb_frame.f_code.co_name == 'throw':
74 reraise(exc_type, exc, last_pony_tb)
---> 75 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback
76 finally:
77 del exc, tb, last_pony_tb
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs)
58 return func(*args, **kwargs)
59
---> 60 try: return func(*args, **kwargs)
61 except AssertionError: raise
62 except Exception:
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in __get__(attr, obj, cls)
2651 def __get__(attr, obj, cls=None):
2652 if obj is None: return attr
-> 2653 if obj._status_ in del_statuses: throw_object_was_deleted(obj)
2654 rentity = attr.py_type
2655 wrapper_class = rentity._get_set_wrapper_subclass_()
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in throw_object_was_deleted(obj)
4134 assert obj._status_ in del_statuses
4135 throw(OperationWithDeletedObjectError, '%s was %s'
-> 4136 % (safe_repr(obj), obj._status_.replace('_', ' ')))
4137
4138 def unpickle_entity(d):
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in throw(exc_type, *args, **kwargs)
96 raise exc
97 else:
---> 98 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback
99 finally: del exc
100
OperationWithDeletedObjectError: B[1] was marked to delete
oh sheee
In [39]: B[1].delete()
In [40]: commit()


Alexander
10.04.2017
17:20:29
Ok, I reproduced it, thanks. Give me a few minutes
I agree that this behavior is counter-intuitive. I need to think how to fix it.
Probably we need to allow cascade_delete=True options for many-to-many attributes

stsouko
10.04.2017
17:43:16
Yes. Previously I try to set cascade delete for m2m.
This is good feature: unsimmetric delete. Imho

Alexander
10.04.2017
17:44:33
agree

Святослав
10.04.2017
18:14:03
bydlocoding folder, lol

Luckydonald
11.04.2017
07:06:41
What is the efficient way to just get all elements of a table?
In terms of memory and speed, I mean

stsouko
11.04.2017
07:26:30
m.b. .page() method usefull?

Alexander
11.04.2017
07:47:08
page_size = 1000
page_num = 1
while True:
with db_session:
objects = MyObject.select(). \
order_by(MyObject.id). \
page(page_num, page_size)
process_objects(objects)
if len(objects) < page_size:
break
page_num += 1


Jordi
11.04.2017
12:12:57
Hello guys. I'm trying to write a function that lets me create the classes dinamically
def table_class(cls_name, table_name, **kwargs):
# reset current mapping
db = Database()
class A(db.Entity):
_table_ = table_name
A.__name__ = cls_name
for attr, attr_def in kwargs.iteritems():
a_def, a_type, a_name = attr_def
setattr(A, attr, a_def(a_type, column=a_name))
# mapping new class
db.bind('mysql', host=None, user=None, passwd=None, db=None)
db.generate_mapping(create_tables=False)
globals()[cls_name] = A
return A
# —-
definition = {
u'dataId': (PrimaryKey, unicode, 'dataId'),
u'owner': (Required, unicode, 'owner'),
# + some other fields ...
}
table_class('Data', 'data', **definition)
but the code above fails to create the class properly (no defined PrimaryKey), any idea on how I could achive it?


Alexander
11.04.2017
12:34:42
In simple cases (if you don't use composite keys) you can do something like that:
from pony import orm
from pony.orm import core
def define_entity(db, entity_name, attrs):
return core.EntityMeta(entity_name, (db.Entity,), attrs)
db = orm.Database()
entity = define_entity(db, 'Person', dict(
name=orm.Required(str),
age=orm.Optional(int)
))
orm.sql_debug(True)
db.bind('sqlite', ':memory:')
db.generate_mapping(create_tables=True)

Google

Jordi
11.04.2017
13:48:45
Many thanks Alexander.

Richie
12.04.2017
07:10:05
what if my code suddenly dead / crash during processing ? How does pony able to handle ?
I meant, Do the database would be alright?

Alexander
12.04.2017
08:14:17
The database should be alright, Pony use transaction for each db_session and perform rollback if an exception occurred. If Python process crashes, then the database itself should perform rollback

Luckydonald
12.04.2017
08:32:28

Kuma
12.04.2017
10:34:56
hai

Alexander
12.04.2017
10:36:00
Welcome

Kuma
12.04.2017
10:37:31
^^
idk if anyone knows any art groups on telegram

Alexander
12.04.2017
10:39:08
This is another type of Pony ;)

Kuma
12.04.2017
10:41:11

Romet
12.04.2017
11:11:48
pls no

Rozen
12.04.2017
13:46:48
wtf

Swadhin Sangram
12.04.2017
18:45:18
???

Nate
12.04.2017
19:51:26
Hey, quick querying question. I'm new to pony, and I'm trying to go through the query builder rather than drop to sql. I want "SELECT fieldname, COUNT(*) FROM Table GROUP BY fieldname" but can't seem to figure it out.

Alexander
12.04.2017
19:57:11
select((x.fieldname, count()) for x in MyEntity)

Nate
12.04.2017
19:59:41
fantastic. thank you very much

Alan
13.04.2017
03:48:15
this may be a silly question - but is there a way I can have pony add a column to my DB? Or does I need to do that separately and then declare it?

Alexander
13.04.2017
06:19:19
It may be done with migration tool. It should be released soon

Luckydonald
13.04.2017
07:11:03
I'm sorry

Google

Luckydonald
13.04.2017
07:11:28
(Actually, I lied, I'm not sorry)

Святослав
13.04.2017
07:27:33
Offtop: when i saw all this pictures i think "My Little Pony ORM", it's so cute

Romet
13.04.2017
10:54:12
Can we not bring MLP into this please

Richie
13.04.2017
12:14:45
What is MLP?
I tried to make pony orm combine with Wxpython...hope it will work flawless
I have to separate each event in wxpython onto @db_session...
Wish mr luck
-*me

Luckydonald
13.04.2017
13:31:53
MLP Season 8 airing in about an hour!
https://cytu.be/r/TheBronyNetwork

Janis
16.04.2017
12:31:29
Any idea how to setup my relation ships of the entities? https://pastebin.com/AiPG8S9h

Alexey
16.04.2017
12:35:41

Janis
16.04.2017
12:37:57
Of course I've read the doc but I don' get this running.

Alexey
16.04.2017
12:39:22
You need to specify the other side of the relationship in the Server entity

Juacy
16.04.2017
12:39:39
hello guys... can i use pony to store password hashes?
i use pony+bcrypt

Janis
16.04.2017
12:41:50
@alexeymalashkevich https://pastebin.com/raw/LuZdc5bD I've included the user_id but somehow it uses always "User" as name, so I get: pony.orm.core.ERDiagramError: Reverse attribute for CounterStrike_Server_Go.user not found

Alexey
16.04.2017
12:41:51

Google

Alexey
16.04.2017
12:43:24
Sorry, in the User entity

Juacy
16.04.2017
12:43:31
i use uuid for id, but i can use for password too?

Janis
16.04.2017
12:44:34
Isn't there a way to get rid of this? I only need the relation from server to user.

Alexey
16.04.2017
12:49:08

Janis
16.04.2017
12:52:19
I don't get it. How to setup this simple relationship. 1:n between users->servers and 1:1 between server and user. current code: https://pastebin.com/raw/jAxA16b6

Juacy
16.04.2017
12:52:27

Alexey
16.04.2017
12:53:04
@jaheller could you describe your scenario, why you want to declare just one side?

Janis
16.04.2017
12:53:29
Of course it needs them, I thought pony would "assume" some kind of naming like "use_id" like many frameworks did regarding enities
@alexeymalashkevich I just need to get the user to each server when I select a server.

Juacy
16.04.2017
12:57:27
i am a begginer in programming

Alexey
16.04.2017
12:58:26