Volbil
I couldn't get it to work
Volbil
But I found workaround for ordering by object id instead of attribute of object
Lucky
Lucky
Okey, this is a tough one.
I just migrated my tables to basically group similar `telegram_id`s.
So I used the following code to figure out if a channel is marked NSFW (1 = allow NSFW, 0 = spoiler, -1 = don't show NSFW)
or Safe For Work:
(1 = allow SFW, 0 = spoiler, -1 = don't show SFW)
channels = orm.select(channel for channel in ChannelSource if not channel.disabled and channel.allow_nsfw == CHANNEL_RATING.ALLOW.value or (channel.allow_nsfw == CHANNEL_RATING.SPOILER.value and channel.allow_safe in (CHANNEL_RATING.SPOILER.value, CHANNEL_RATING.HIDE.value)))
Now I try to rewrite that to figure out if a TelegramChannel that as well. Basically NSFW can be treated as max aggregation, because if one source is nsfw, you have to expect nsfw. For safe it is the other way around, min that is if all allow safe.
Lucky
I think I can get the allow_nsfw part done with attribute lifting, somehow similar to that:
channels = orm.select(channel for channel in TelegramChannel if not channel.disabled and max(channel.channel_sources.allow_nsfw) == CHANNEL_RATING.ALLOW.value or (max(channel.channel_sources.allow_nsfw) == CHANNEL_RATING.SPOILER.value and min(channel.channel_sources.allow_safe) in (CHANNEL_RATING.SPOILER.value, CHANNEL_RATING.HIDE.value)))
But now I don't know how I can filter out everything which is disabled…
Lucky
Can I use … if all(<condition> for source in channel.channel_sources) ?
Lucky
In other words, iterate over the Set attribute?
Lucky
If it works, I could get away with
channel: TelegramChannel
source: ChannelSource
channels = orm.select(
channel for channel in TelegramChannel if
not (show_disabled or channel.disabled) # hide disabled channels
and channel.channel_sources # hide channels which have no sources
and all( # all sources must conform to:
source.disabled # either be disabled, and thus not influence the result
or source.allow_nsfw == CHANNEL_RATING.ALLOW.value # must allow nsfw
or ( # NSFW is spoilered, but safe is also spoilered or not even shown.
source.allow_nsfw == CHANNEL_RATING.SPOILER.value # NSFW is allowed, but spoilered
and source.allow_safe in (CHANNEL_RATING.SPOILER.value, CHANNEL_RATING.HIDE.value) # and safe is also only spoilered or even hidden
)
for source in channel.channel_sources
)
)
Muhammed
Hi, I trying to make another filter in filter but I can't be successful.
I have two table, Transaction and Log. My main table is Transaction and I use the Log table for operations such as creation, updating, deletion on the Transaction.
Tables are;
class Transaction(db.Entity):
id = PrimaryKey(int, auto=True)
...other_columns...
logs_set = Set(Log, cascade_delete=True)
class Log(db.Entity):
id = PrimaryKey(int, auto=True)
time = Required(datetime, default=lambda: datetime.utcnow())
type = Required(int)
...other_columns...
transaction_ref = Optional('Transaction')
class TYPE:
CREATE = 1
UPDATE = 2
DELETE = 3
My goal is to receive Transaction not marked as deleted. The queries I tried is
cash_box.transactions_set.filter(lambda t: t.logs_set.filter(type=Log.TYPE.DELETE).count() == 0)
I'm getting this error:
File "/home/myilmaz/Clouds/GitHub/myilmaz-web/venv/lib/python3.7/site-packages/pony/orm/sqltranslation.py", line 1401, in wrapper
return method(monad, *args, **kwargs)
TypeError: filter() got an unexpected keyword argument 'type'
How I can do this. Can you help me?
Volbil
I believe type is reserved Python keyword
Volbil
Maybe this cause your issue?
Anonymous
Volbil
Ah, you right
Volbil
Its function https://docs.python.org/3/library/functions.html#type
Muhammed
Yes, I tried filtering with another column and I got an error again
Volbil
Shouldn't there be lambda function?
Muhammed
oh, thank you so much. That was the problem.
Alexander
Can I use … if all(<condition> for source in channel.channel_sources) ?
No, currently you can't because SQL does not have FOR ALL clause. But you can express it with double nested negation with NOT EXISTS clause. The resulted query will look pretty complex and non-intuitive (it is hard to understand an exists clause with double negation), but it should work. Something like
orm.select(
channel for channel in TelegramChannel
if not channel.disabled
and channel.channel_sources
and not exists(
source for source in channel.channel_sources
if not (
source.disabled
or source.allow_nsfw == ALLOW
or (source.allow_nsfw == SPOILER and source.allow_safe == SPOILER)
)
)
)
)
Basically, "select a channel for which does not exist a source which does not satisfy necessary conditions".
Andrey
I'm a new, help me please with select. I need all records from P with specific R and specific M with order by time.
Class R
id = PrimaryKey(int, auto=True)
name = Required(str)
prop = Required(str)
ps = Set('P')
ms = Set('M')
Class M
id = PrimaryKey(int, auto=True)
mid = Required(str)
rs = Set(R)
Class P
id = PrimaryKey(int, auto=True)
time = Required(datetime)
v1 = Optional(str)
v2 = Required(str)
r = Required(R)
Alexander
I understand if you want P related to specific R, as they have relationship. But what do you mean by "with specific M"? P is not connected to M, and if you already have "specific R" I don't see how you plan to further restrict it to "specific M"
Andrey
«specific mid»
Andrey
PonyORM created a table m_r but I cant link to it in the request
Alexander
It may be something like:
specific_r = R[some_id]
query = select(p for p in P if p.r == specific_r).order_by(lambda p: p.time)
or
specific_m = M.get(mid=some_value)
query = select(p for p in P if specific_m in p.r.ms).order_by(lambda p: p.time)
Genesis Shards Community
hi, good: one problem: pony.orm.core.BindingError: Database object was already bound to MySQL provider, why is solution?
Alexander
Genesis Shards Community
now: TypeError: No database converter found for type <class 'pony.orm.core.PrimaryKey'> help mw, please!
Genesis Shards Community
why is solution?
Alexander
You probably specified something like attr = Required(PrimaryKey), don't do that
Genesis Shards Community
Alexander
Andrey
Alexander
Andrey
only this query in db_session or you need a model code ?
Alexander
Probably a query should be enough
Alexander
But it is better to have a code which can be executed
Ben
Hi guys! Is there any known issues with using the "coalesce" function within @property?
Ben
I'm having an issue when Im reading a Decimal value from an object and its fine with the correct resolution
Ben
like:
Ben
print(str(a.value)) # prints 0.00
Ben
(value is a Decimal field with 2 digits after the comma)
Ben
but if I do:
print(a.property_value) # this @property uses a coalesce
print(a.value) # prints 0.0000 even though the resolution is 2
Ben
actually I clarified the issue
Ben
If I do:
print(a.fk_object.value) # prints a string from that object
print(a.value) # prints 0.0000
Ben
it has nothing to do with @property and @coalesce it seems
Ben
but just with decimal when playing with other objects that have variables with higher decimal values
Alexander
I'm not sure this is the actual reason. Can you try this sequence:
print(str(a.value))
print(a.value)
print(a.fk_object.value)
print(a.value)
Ben
that works
Ben
if I read the value before accessing the foreign key
Ben
Yup
Ben
so what you said works, but this fails:
print(a.fk_object.value)
print(a.value)
Ben
even just doing print(a.value) before works
Ben
and makes it so that the second print is correct
Ben
then the value is correct
Ben
it seems like the computer gremlins are at it again, this a very odd issue haha
Alexander
It looks strange. What database do you use?
Alexander
Is it SQLite?
Ben
Postgres
Ben
the fk_object has a variable that is decimal and of resolution 4
Ben
could be related to that
Anonymous
hi guys,
might be a simple question.
How do we do fuzzy search in pony orm ?
I have a q=tshirt and would like to search my Product db entity with 3 fields (name, description, tag).
In django, it would be like q((name__icontains='tshirt')|(description__icontains='tshirt ')|(tag__icontains='tshirt'))
Alexander
Probably
param = tshirt
select(x for x in X if (
param in x.lower() or param in x.description.lower() or param in x.tag.lower()
))
Alexander
But for real user queries it is probably better to use full-text-search queries
Alexander
Pony does not have out of the box support for full text search queries, but you can add necessary tables manually
Anonymous
thank you very much!
Christian
Just playing around with orm.migrations and it works great for me! 👍 But it took me quite a bit of searching to figure out how to install it: https://github.com/ponyorm/pony/issues/16#issuecomment-400161683
Christian
Maybe it could be useful to advertise this more?
Alexander
Migrations are not ready yet. orm-migrations is previous attempt. New branch is migrations_dev. Both are incomplete at this moment...
Alexander
You can try to use it on your own risk, but some important features may be missing
Christian
Yeah, I saw migrations_dev. It seems good enough for my simple application.
Christian
Anyways, just saying thanks for the work you already did on it!
Alexander
You're welcome!
Christian
I'm really happy the pony is still alive and kicking :) Keep it up!
Alexander
😊
Anonymous
Pony is so awesome. It doesn't get in my way.
Anonymous
hey guys 🙂 @metaprogrammer may I ask you what library you used for the connecting svg-lines/arrows? https://editor.ponyorm.com/user/pony/PhotoSharing/designer it looks fabulous 🤌
Alexey
Anonymous
so you wrote that algo on your own?
Alexey
Yep
Anonymous
that’s wild =)