@ponyorm

Страница 56 из 75
Александр
31.01.2018
15:04:59
I can reorganize the code) I will not distract you.

Alexander
31.01.2018
15:06:37
Okay. Make it single select()

Alexander
31.01.2018
15:07:07
Currently, it is not possible to split OR between several lambdas. You need to write OR expression inside a single lambda a = a.where(lambda p: "14" in p.name or 14 == p.id)

Александр
31.01.2018
15:11:31
Thanks again!!!

Google
Александр
31.01.2018
15:11:40
?

Hi. My designer does not save the changes "Cascade Delete"

http://joxi.ru/a2XQlbGC1BeB4A

I put False in all tables and saved.

And opened a new window designer!

All tables are Default (True)!

Bug?

Roman
01.02.2018
17:02:14
yes. it is.

Matthew
02.02.2018
09:46:24
Hi, Since raw primary keys can be used when creating an object and referencing a foreign key, would it make sense to allow the same for queries such as this: Review.select(lambda r: r.product == 1).count()

Alexander
02.02.2018
10:46:09
Hi Matthew, currently you can write it explicitly: Review.select(lambda r: r.product.id == 1).count() For me, it doesn't look too verbose, so I'm not sure we need to add implicit casting of id to specific object type

Matthew
02.02.2018
10:48:57
Agree, with .id it works fine :)

Александр
02.02.2018
17:09:55
Hello. An incomprehensible mistake. I got the base. Pony writes an error when creating.

TypeError: Incorrect type of attribute: ?.?

Google
Александр
02.02.2018
17:10:09
class Order(db.Entity): id = PrimaryKey(int, auto=True) date = Required(date) date_shipping = Optional(date) sum_shipping = Optional(Decimal) sum_position = Required(Decimal) sum_discount = Required(Decimal) sum_all = Required(Decimal) value_position = Required(int) discount_percent = Required(Decimal) issued = Required(bool) note = Optional(str) client = Required('Client') shipping_method = Required('ShippingMethod') payment_method = Required('PaymentMethod') order_positions = Set('OrderPosition', cascade_delete=False) pre_order_positions = Set('PreOrderPosition', cascade_delete=False)

date_shipping = Optional(date) On this line!

https://pastebin.com/x8hKAi5P

I swapped the lines and everything worked !!! Why?

date_ship = Required(date) date = Required(date)

Video how it works!

Alexander
02.02.2018
20:28:01
The first version of code fails because you redefined date name meaning from date type to Order.date attribute, and then tried to set date (that is, Order.date) as a type for date_shipping attribute. At the moment when the exception is generated Pony does not know yet what is the attribute name, because Order class was not created yet and attribute descriptors didn't receive their name from the Order class initialization code

Александр
02.02.2018
20:31:38
Didn't know what it could be!

Thanks.

Yuri
04.02.2018
15:18:00
Hello. I remember there was a framework called PonyJS. But now I can`t find any docs and repo for it. Is it still actual?

Alexander
04.02.2018
15:19:49
Hi, at this moment it is not actual, I hope we can revive it later.

Yuri
04.02.2018
15:21:01
But why?

Alexander
04.02.2018
15:39:22
We developed PonyJS during the work on fineartbiblio.com. It was successful for this project, but we discover many corner cases where the code didn't work properly. We were able to use it anyway, because we know internal details of PonyJS implementation, and were able to write workarounds. But it will be too complex and time-consuming to explain all this corner-cases and workarounds to other developers. Because of that, we decided to not release public version of PonyJS. We got useful experience from our internal usage of PonyJS, and I think we have understanding how to write "PonyJS 2.0" properly, so it would be usable for all developers. But right now we just don't have time and resources to develop PonyORM and PonyJS in parallel. After we complete most important tasks related to PonyORM, we can start new implementation of PonyJS based on our previous experience.

Yuri
04.02.2018
15:54:42
Well then I'll wait. Hope you got it all. :)

Ehsan
04.02.2018
20:03:39
Hi guys

Do you know how I can delete a class in pony?

Alexander
04.02.2018
20:04:36
Hi! What do you mean by "delete a class"?

Ehsan
04.02.2018
20:04:48
Like: Class person (dB.entity):...

Alexander
04.02.2018
20:06:02
Do you already have a database filled with useful data that you don't want to lose, or you can just re-create a fresh database?

Ehsan
04.02.2018
20:06:28
I want to create a fresh database

Google
Ehsan
04.02.2018
20:06:38
*recreate

Alexander
04.02.2018
20:10:43
You can clear all tables from the database. To do this, call db.drop_all_tables(with_all_data=True) or you can manually delete all tables using some dbAdmin interface If you use SQLite, instead of these, just delete the database file. Then you can modify your entities, and when you start your program again, correct tables will be created automatically

The tables will be created if db.generate_mapping call has option create_tables=True

Ehsan
04.02.2018
20:13:04
I think I have no mapping cause I get this error:

ERDiagramError: No mapping was generated for the database

Alexander
04.02.2018
20:14:18
after you define all models, in your main program you need to have the following line: db.generate_mapping(create_tables=True)

This is the moment where Pony determines which tables are necessary to represent all specified entities, and checks that all tables are presented inside the database.

Ehsan
04.02.2018
20:15:38
What did I miss? MappingError: Database object is not bound with a provider yet

Alexander
04.02.2018
20:17:47
Before generate_mapping call, you need to specify database parameters to connect, such as host, login, password, etc. You can do it inside Database(...) constructor or inside db.bind(...) method. The parameters are specific to DBMS you use

You can find pony/orm/examples/university1.py example and look at db.bind(...) and db.generate_mappping(...) calls

Ehsan
04.02.2018
20:19:43
I just creasted one and run the db.generate_mapping(create_tables=True)

I got this error: ERDiagramError: Entity definition Usage was not found

Alexander
04.02.2018
20:21:24
You have an attribute like something = Required("Usage") in some of your models, and don't have Usage class definition

Ehsan
04.02.2018
20:23:17
Right!

Hi, I am trying again to delete a database and keep getting this error:

MappingError: Mapping was already generated

I already executed these commands:

db.generate_mapping(create_tables=True) db.drop_all_tables(with_all_data=True)

Found the solution, thanks!

Hi Ngalim! db.bind(...) is used to specify database type and parameters of database connection. db.generate_mapping(...) is used to notify Pony: "all necessary entities are defined". At this moment Pony understands which tables should be used to represent specified entities. It is possible to specify create_tables=True option to generate tables, but even if tables are already created, calling generate_mapping is necessary, because this is the moment when Pony creates internal linking between entities and tables. When application is imported, the sequence of operations should be the following: from pony import orm db = Database() class X(db.Entity): ... class Y(db.Entity): ... db.bind(...) db.generate_mapping(...) with db_session: result = select(...) That is, generate_mapping should be called after all entities are defined, but before first select is issued. If you want to put entities in a separate module, I suggest you to define db right in that module, but perform bind and generate_mapping outside of it. This way you separate entity definitions and database configuration options. It may looks something like that: models.py: db = Database() class Person(db.Entity): name = Required(str) controllers.py: from flask import Blueprint from model import db, Person employee_bp = Blueprint(...) main.py: from flask import Flask from controllers import employee_bp from models import db app = Flask(__name__) ... app.register_blueprint(employee_bp) if __name__ == '__main__': db.bind(...) db.generate_mapping() app.run(...)

Google
Matthew
05.02.2018
09:49:32
Is there a way to update one attibute on an entity without Pony validating that all other entities are the same? I think I asked this before but I'm afraid I can't remember the answer

I have the primary key of the entity

I don't need to load the full data of the entity, just one field, then update that one field

x = X[id]

x.y = x.y * 2

is the equivalent of what I'm doing

Alexander
05.02.2018
12:43:38
Matthew, if you are sure that the object with that id really exists, you can do the following: x = X._get_by_raw_pkval_((id,)) x.load(X.y) # or x.load("y") x.y = x.y * 2 But I doubt loading one attribute will be much faster then loading all the non-lazy attributes

Matthew
05.02.2018
13:11:18
The problem is not the loading of attributes, the update query is extremely slow. This is Postgres being stupid with indexes but it would be nice to avoid the complex update query

Alexander
05.02.2018
15:03:21
Can you explain in more details, what in the update query text causes the slowdown in your case? Is it optimistic checks or what?

If you update just one column, then the update query should write this specific column only

Matthew
05.02.2018
16:05:03
It is making sure that all other columns still have the same values, I think it is a pony sanity check?

Like update x set y=5 where I'd=1 and z="existing value"

Alexander
05.02.2018
16:10:53
It is to be sure that the value of z column was not modified by concurrent transaction. If your transaction do the following: a) retrieve x object b) read x.z value c) set x.y value to 5 Then it is reasonable to assume that the value of x.z attribute was somewhat important for calculating a new value of x.y attribute. If another concurrent transaction modified x.z before your current transaction saved new value of x.y, then Pony assumes that you probably need to recalculate x.y

Matthew
05.02.2018
16:12:09
Ok, is there a way to bypass this without writing raw sql?

If I don't read the attributes, will this protection not happen?

Alexander
05.02.2018
16:13:36
Yes, if it is possible to not read irrelevant attributes in application code, then these attributes will not be added to optimistic checks.

Matthew
05.02.2018
16:13:45
Aha cool

I can cache those in redis easily

I am comparing one object to another from the recent past

Alexander
05.02.2018
16:15:01
Also it is possible to turn off optimistic checks by specifing optimistic flag in db_session: db_session(optimistic=False) But I consider it as a last resort option, because turning optimistic checks off may lead to lost updates from concurrent transactions

Google
Alexander
05.02.2018
16:19:12
I am comparing one object to another from the recent past
It seems you are not the only user who are doing something like that. Maybe we need to support some specific workflow for this use-case, I mean, comparing a bag of attribute values for the object with their past values

Also, we can add another type of optimistic check. Instead of comparing all columns with their previous values, we can add support for version column which value will be incremented on each update. Then we need to compare the version column only. But the current approach has the benefit that concurrent transactions can update different columns x.a and x.b in parallel without any errors, whereas using version column can produce some false conflicts for such updates

Matthew
05.02.2018
16:26:44
For me I am comparing the values of an object currently in the database with the values of an object that I am considering adding to the database, basically duplicate detection based on custom logic

if the new data is considered duplicate, the values for new and existing are averaged for one column

x.y = (new_x.y + x.py) / 2

Alexander
05.02.2018
16:28:35
What you are considering duplicates? An objects with all attributes equal?

Matthew
05.02.2018
16:28:44
most attributes equal

Alexander
05.02.2018
16:29:05
So, in that case, id column is not really id

Matthew
05.02.2018
16:29:24
the ID of the existing object stays static

ID is one of the attributes that I filter out yeah

Alexander
05.02.2018
16:30:09
Ok, and what is the types of other attribues of these objects? int, str, what else?

So, all columns except x.y are natural key, and x.y is the counter?

Matthew
05.02.2018
16:31:38
date, string, foreign key, int, float

I am fine with just caching the existing object data in redis for my use case

Alexander
05.02.2018
16:34:22
When you have new object you need to be sure that this object is not in the database and then insert it. So, you have query like x = X.get(a=a, b=b, c=c, ...) for all attributes except id and y?

Then you need to have a composite key for all this attributes

Matthew
05.02.2018
16:36:56
I am worried that would lead to a lot of resource usage, this table has 120 million rows

so that would be a massive index for the composite key?

Страница 56 из 75