Anonymous
Just Because I read is the best practice. But rather I prefer just use an int for id as you said.
Alexander
It is better to use object because it is more safe
Alexander
Maybe the problem is caused by some bug in Pony. It would be helpful if you can show me the code that creates objects. If it is confidential you can send it as a personal message to me
Anonymous
Is not confidential.
Alexander
Ok, then show the code of add_voucher method
Anonymous
I'll paste the code here if isn't a problem. (With pastebin of course)
Alexander
As well as get_offer method
Alexander
Sure
Lucky
Anonymous
Https://pastebin.com/LeGhyfnb
Anonymous
Tell me If you need to run the code. You will need the db.cvs file
Anonymous
But it has private data. So I will have to fake it.
Alexander
Ok, I'll just look at code at first
Anonymous
When instantiating an entity with a foreign relation, can I specify the foreign key instead of passing another entity?
Alexander
Yes, I just wrote about that
Alexander
Pony allows to specify object id instead of object itself:
phone2 = Phone(person=777, number='234-56-78')
When Pony sees id value instead of object, it assumes that the object with that id value already exists inside the database. If this is not true, then the database will raise the error that you encountered.
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
Anonymous
That's great, thanks!
Matthew
Is there an alternative to upgrading my Postgres 9.3 install to be able to use JSON with Pony?
Matthew
Nevermind
Александр
Hello.
Tell me why it does not work?
a = select(p.name for p in Parts)
a = a.filter(lambda p: "20" in p.name)
print(a[😏)
AttributeError: 'str' object has no attribute 'name'
Александр
print a[ : ]
Alexander
Hello!
Alexander
Cause lambda argument for filter function is p.name in your case.
Alexander
Filter always gets what was the X in select(X for ...
Александр
Can I make it this way?
a = select(p.name for p in Parts if "20" in p.name)
Alexander
If you have version of pony 0.7.3 you can use where instead of filter, it will work as you want
Alexander
Ofc you can
Alexander
Wait
Александр
GOOD
a = select(p.name for p in Parts)
a = a.where(lambda p: "20" in p.name)print(a[😏)
Alexander
Or not, need to check
Александр
a = select(p.name for p in Parts)
a = a.where(lambda p: "20" in p.name)
print(a[😏)
It works
Alexander
Okay then.
Alexander
In query
q = select(x.a for x in X if ...)
q = q.filter(lambda arg: ...)
q = q.where(lambda p: ...)
The argument arg of q.filter refers to x.a, the result of select expression (the name of argument arg may be arbitrary)
The argument p of q.where refers to loop variable name p of original select generator
Александр
understandably! Thank you!
Александр
Another question
Why if
a = select(p.name for p in Parts)
text = "12"
a = a.where(lambda p: text in p.name)
QUERY : FROM parts p
WHERE p.name LIKE concat('%%', replace(replace(replace(%s, '!', '!!'), '%%', '!%%'), '_', '!_'), '%%') ESCAPE '!'
['12']
if
a = select(p.name for p in Parts)
a = a.where(lambda p: "12" in p.name)
print(a[😏)
QUERY : FROM parts p
WHERE p.name LIKE '%%12%%
Александр
why is added WHERE p.name LIKE concat('%%', replace(replace(replace(%s, '!', '!!'), '%%', '!%%'), '_', '!_'), '%%') ESCAPE '!'
Alexander
Cause adding string variables right in sql query is dangerous
Alexander
And it works different in pony, when pony sees constant and global variable
Alexander
But tons of replace seem ugly, I agree)
Александр
anti sql injection ?
Alexander
Yup
Александр
Affects the speed of the slave?
Alexander
Slave?
Александр
Affects the speed of work?
Александр
Excuse me)
Alexander
Because in SQL in order to check that a substring is a part of string you need to use LIKE operator. And LIKE operator has escape symbols % and _ which are treated in a special way. _ means "any symbol" and % means "any number of any symbols"
If a substring is received from a column, it is possible that the substring contains these symbols. And then the result of LIKE would be incorrect. In order to prevent that, we need to replace each % to !% and each _ to !_. Then !% will be interpreted as just a single % symbol without any special meaning. The same is with _
In other words, this expression is the simplest expression which works corectly when p.name contains % or _ symbols
Alexander
More actions always produces more additional time. But I think here you dont lose much. Anyway this things are important.
Александр
I understand. Thank You! You, well done)
Александр
Last question!
Excuse me!
I can not find it in the documentation.
a = select(p.name for p in Parts)
a = a.where(lambda p: "14" in p.name)
a = a.where(lambda p: 14 == p.id)
print(a[😏)
WHERE p.name LIKE '%%14%%'
AND 14 = p.id
How to change AND to OR?
Alexander
You should reorganize your code. You can chat me in PM, I can explain you there using our native language)
Александр
I can reorganize the code) I will not distract you.
Alexander
Okay. Make it single select()
Alexander
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)
Александр
Thanks again!!!
Александр
👌
Александр
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
yes. it is.
Matthew
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
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
Agree, with .id it works fine :)
Александр
Hello.
An incomprehensible mistake. I got the base. Pony writes an error when creating.
Александр
TypeError: Incorrect type of attribute: ?.?
Александр
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
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
Александр
Didn't know what it could be!
Александр
Thanks.
Yurii
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
Hi, at this moment it is not actual, I hope we can revive it later.
Yurii
But why?