Anonymous
Lucky
just remove auto=False, then
Anonymous
Okay
Anonymous
Hmm but am not sure
Anonymous
Lucky
I use pytgbot.
Anonymous
Yes ptb
Alexander
If User is another entity, then you probably need to create relationship instead
user_id = Optional(User)
Alexander
and define reverse attribute on User side
Alexander
> class notification(db.Entity):
entity name should be in uppercase - Notification
Anonymous
Anonymous
Anonymous
What's User there
Anonymous
I dont have User entities
Alexander
in Python class names should be in Uppercase
Alexander
Ok, if you don't have User entity it may be just user_id
Anonymous
class Notification(db.Entity):
chat_id = PrimaryKey(int, auto=False, size=64)
user_id = Optional(int, default=None, size=64)
full_name = Optional(str, default='')
Like this?
Alexander
class Notification(db.Entity):
id = PrimaryKey(int, auto=True)
chat_id = Required(int, size=64)
user_id = Optional(int, size=64)
full_name = Optional(str)
Anonymous
Lucky
maybe in your case making both chat_id and user_id a PrimaryKey would make sense.
Alexander
It is necessary if you want size other then default (in PostgreSQL default is 32)
Anonymous
Alexander
If chat_id is primary key then you cannot have more than one notification for a specific chat
Alexander
If it is correct, you can make chat_id the primary key
Anonymous
Anonymous
So i can do
users = list(Notification.select(lambda x: x.chat_id==999))
for user in users:
Send_message(user.user_id, 'hi this is notification')
users.select(lambda obj: obj in users).delete(bulk=True)
Alexander
> for user in users:
> I dont have User entities
Probably you should?
Anonymous
Lucky
Yes ptb
Oh, ptb is short for pytgbot?
We're both speaking about https://github.com/luckydonald/pytgbot, yes?
Anonymous
Anonymous
Different
Lucky
I use pytgbot.
Lucky
Yes.
Anonymous
Oh
Lucky
Currently working on bringing py3 asyncio support to it.
Because boy, that stuff is fast.
Lucky
link = JoinLink.select(lambda link: link.of_chat == linking.chat).order_by(orm.desc(JoinLink.date_added)).limit(1)
if link.replaced_by is not None
…
AttributeError: 'QueryResult' object has no attribute 'replaced_by'
Lucky
I can't figure out how to get a row from that.
Lucky
I require assistance! https://docs.ponyorm.org/search.html?q=QueryResult
Lucky
AttributeError: 'QueryResult' object has no attribute 'get'
Lucky
I think I got it to work with link._query._actual_fetch(1,0) …
Lucky
But that feels dirty..
Lucky
No, that doesn't work
Lucky
How the heck to I get a simple result, or None?
Alexander
Use .first() instead of .limit(1)
Alexander
QueryResult is basically an array of objects, but it may be lazy, so fetching happens on first access
Alexander
If you already have QueryResult you can use [0] to access the first object in a list
Lucky
does .first also makes sure I have exactly 0 or 1 result, like get would?
Matthew
I don’t think so
Lucky
That's super strange.
Lucky
It should have the same methods as Query
Lucky
or at least be documented?
Lucky
Does first() return None if nothing is found?
Alexander
Alexander
The difference between get() and first() is that get throws MultipleObjectsFoundError if more than one objects were returned
Lucky
So you probably mean the other way around?
Alexander
Both get and first returns None when no objects were found
Alexander
The difference is when more than one objects were found
Lucky
Oooh. Now I get it.
Evgeniy
Hi! I'm using sqlite. How do I specify filename in the bind() method so that the path is written not relative to the directory where the model is located, but relative to the base directory of the script in python?
Anonymous
Ah
Notification(chat_id=123, user=User(user_id=321, full_name='abc'))
Notification.get(chat_id=123).user.user_id
Like that?
Lucky
Maybe something like this:
for user in Notification.user.filter(lambda user: user.chat_id==123):
user.user_id
Lucky
Not sure if that's correct code.
Lucky
For some reason .filter() breaks the attribute chain
Anonymous
Lucky
I found a bug:
SomeTable.select(lambda t: t.somefield is not True)
will not work in postgres if the value is NULL.
Because it generates the query
"SomeTable"."somefield" <> true
but it would need to create
`"SomeTable"."somefield" <> true OR "SomeTable"."somefield" IS NULL
to have a valid database representation of the python logic.
Lucky
Matthew
use “not t.somefield"
Lucky
While that is a valid workaround, the one above still is a bug.
Lucky
Because this is a "pythonic orm", so it should reflect the python logic.
Matthew
I think that explicitly comparing to a boolean implies that you are comparing booleans, and not testing if it is null
Lucky
The Zen of Python, 2nd rule: Explicit is better than implicit.
Lucky
And using basically the same somefield is not True lambda, both False and None are selected.
Matthew
Maybe best to have columns that don’t allow null (Required) 🙂
Evgeniy
Can I use parts of the Pony documentation as documentation for my projects? I mean, copy them. From the point of view of the license.
Alexander
I think yes
Chris
Is there a way to represent a Pony Set as a dict?
Jim
out of the box I'don't think but can still build it yourself with a dict comprehension :
mycollection = {it.pk:it.to_dict() for it in school.students.select()}