Alexander
But generally you can just wrap separate units of work with different db_session s
Anonymous
Thanks that did the trick :)
Anonymous
Hi everybody!!!
Anonymous
My name is Germán. I'm from Venezuela and I love PonyORM
Matthew
Hi :)
Anonymous
I have a question
Matthew
What is it?
Anonymous
I can't get why this error...
Reverse attribute for Voucher.oferta_id not found
Matthew
Paste your models, maybe on http://gist.github.com/
Anonymous
Ok!
Anonymous
Https://pastebin.com/kvv4WY2K
Matthew
Add this to Offer: voucher = Required('Voucher')
Matthew
By the way, I think it's better to remove "_id" from attributes, as you are referencing an object, not an ID of an object
Anonymous
Oh!!! 😅 Thank a lot!!!
Alexander
Hi Germán! I think you need to fix some relationship attributes. For example, in the following definition
```
class Reservation(db.Entity):
...
deposit_tx = orm.Optional(str)
guest_id = orm.Required(Guest)
offer_id = orm.Required(Offer)
...
```
It is incorrect to name attribute `guest_id`, because in Pony its value is Giest object itself and not its id value. It is implemented using a column which keeps id value, but in Python you will get Guest object when accessing this attribute. The same is with `offer_id` attribute. So the code should look like
```
class Reservation(db.Entity):
...
deposit_tx = orm.Optional(str)
guest = orm.Required(Guest)
offer = orm.Required(Offer)
...
```
If for some reason you want the column name which ends with _id, you can specify it using `column` option:
```
class Reservation(db.Entity):
...
deposit_tx = orm.Optional(str)
guest = orm.Required(Guest, column=guest_id)
offer = orm.Required(Offer, column=offer_id)
...
```
Anonymous
Oh Great!!! Thank a lot!!!! Nice group guys. One point more to love ponyorm even more.
Alexander
Each relationship attribute should have corresponding reverse attribute in another entity. If one entity Foo has attribute
bar = orm.Required("Bar")
then Bar entity should have something like
foos = orm.Set("Bar")
Matthew
By the way, Alexander is the creator of Pony, he's probably too modest to say :)
Александр
😀
Anonymous
😱
Anonymous
Alexander thank you for such great thing. I really ❤ Ponyorm
Alexander
You are welcome! :)
Anonymous
Good morning guys!
Anonymous
One question. The entity relationship may be with an Optional?
Anonymous
Ok I saw it!
Alexander
Good. Yes, it stands for 'Zero or One'. As I remember.
Anonymous
Yes. I found it in the docs. Thank you again.
Anonymous
Hello again 👋
Anonymous
I have to insert two billion rows into a DB so I'm trying to speed up every part of the process.
I've noticed the initialization of instances is a big bottleneck. In particular cut_traceback and core.validate? Do you know how to switch off cut_traceback? And could I switch off validate as well if I'm confident in the integrity of my data?
Jannes
Anonymous
I should not but I don't want to have to add raw sql to my code if I can just change a few config options and gain substantial speed.
Matthew
https://docs.ponyorm.com/api_reference.html
Matthew
Look for the insert method. On mobile so can't direct link
Matthew
insert(table_name|entity, returning=None, **kwargs)
Insert new rows into a table. This command bypasses the identity map cache and can be used in order to increase the performance when you need to create lots of objects and not going to read them in the same transaction. Also you can use the execute() method for this purpose. If you need to work with those objects in the same transaction it is better to create instances of entities and have Pony to save them in the database.
Anonymous
Thanks for the tip. It made the process 1.5 times faster but it won't be enough, looks like I'll have to go with raw sql after all.
Matthew
Are you doing a lot of inserts per transaction?
Anonymous
200000
Matthew
How much faster was raw SQL?
Anonymous
4 times faster than the insert method
Matthew
Are you able to share the code for raw and insert? seems like a surprising difference
Anonymous
Sure hold on
Anonymous
https://gist.github.com/anonymous/8a62f9a41319d6f74094c6af371b6602 here ya go
Anonymous
Forgive the ugly code, it's just the profiling prototype
Anonymous
The difference in speed is probably because I'm able to do a single execute with psycopg2 vs 200000 with pony
Matthew
How many batches of 200k did you do?
Matthew
one?
Anonymous
A few dozens
Anonymous
I only profiled one
Matthew
Matthew
well, one INSERT
Anonymous
yeah
Matthew
Analytics query on dedicated server with postgres 9.3: 47 seconds
same query with postgres 10.1 on VPS (not dedicated cores) with 6 parallel workers and a lot less ram: 4 seconds
Matthew
It seems like a postgres 10 upgrade could be very helpful, are there any downsides?
Alexander
I have not used it yet, probably the swith to Postgres 10 should not have any downsides
Matthew
By the way, it is available from Postgres instead of using the default ubuntu version: https://www.postgresql.org/download/linux/ubuntu/
ՊԲՒՇՍՏ
hello, is it posible to sort a result by an calculation like .order_by(column1 + column2)
Matthew
In [5]: select((u, u.id + 5) for u in User).order_by(2)[😏
Out[5]: [(User[1], 6), (User[2], 7), (User[3], 8), (User[4], 9)]
Matthew
so make it a calculated column, then order by that column
ՊԲՒՇՍՏ
Thanks. Great idea.
Alexander
Also you can write arbitrary expressions in order_by using lambda:
select(x for x in X).order_by(lambda x: x.a + x.b)
Anonymous
Hi guys. I have a stupid problem. But I'm googling it but I can't find an answer good for my case. I'm getting a IntegrityError: 'FOREIGN KEY constraints falided' Error. But I can see why that. Could you give any clue of when this error happen?
Alexander
Hi, do you use some pre-existing tables, or created a new tables with Pony?
Anonymous
It is for a demo_content. So each time I delete the whole file to create it again.
Alexander
Can you show a traceback?
Anonymous
I get the error when I try to populate the table.
Anonymous
pony.orm.core.TransactionIntegrityError: Object Voucher[new:15] cannot be stored in the database IntegrityError: FOREIGN KEY constraints falided
Alexander
It's hard to say what is the reason for the error without looking at full traceback. Maybe you need to show models also. The error took place not in Pony itself, but inside the database, so the error message is pretty general
Anonymous
https://pastebin.com/UBAYR57j
Anonymous
https://pastebin.com/LUXZNvZb
Alexander
Specifying id value instead of object is an optimization trick which can be applied if a programmer does not want load object from the database, and wants to say to Pony: "hey Pony, the object with this id is already created, I guarantee that, don't waste your time for checking this". In general it is better to specify object itself instead of specifying id
Alexander
So you should not specify raw id as a value of relationship attribute without exact reason
Matthew
Didn't know raw ID was possible, cool :)
Alexander
So you did't use them?
Alexander
Ah, ok, it was not you :)
Matthew
Nope, always used User[1] etc but it is rare
Anonymous
The problem that I have is that an offer could be in many instances of vouchers. Maybe my code is broken when I'm trying populate using the same offer to a different voucher.
Anonymous
Thank you for the clue!
Alexander
So you don't use id values as I described before?
Anonymous
I'm using an offer Object to populate voucher.