Anonymous
Hello everyone, is there any instrument/plugin for adoption ponyorm for trio(triopg) ?
Genesis Shards Community
hi, helpme, why upload imagen or picture ?
Jim
how to let Decimal unmodified when using pony. I mean I wan't Decimal('2) stays "2" and Decimal('3,56') stays "3,56". I can't get it using scale and precision.
Jim
or should I store it as string and convet it back ?
Alexander
If you want arbitrary precision for decimals you should store it as string values
Jim
Ok
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Hi friends!
Is there any way to define entities by inheriting another object instead of writing it in code? I suppose an example would explain better:
class Document(db.Entity, telegram.Document):
pass
#insted:
class Document(db.Entity):
file_id = PrimaryKey(str)
file_name = Optional(str)
# and so on..
Nikolay
Nikolay
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
ok, got it
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
what should an object look's like to be able to inherit entitis?
Nikolay
https://docs.ponyorm.org/entities.html#entity-inheritance
It should be a db.Entity subclass
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Is there no other way? Must be just like the object inherited from db.Entity.. no?
Nikolay
Nikolay
So while you can inherit random classess, i don't think you can magically make their attributes produce db columns
Nikolay
Make normal document entity (by hand) and add a from_dict or from_telegram method to it (also by hand) so later you can easily convert incoming telegram update into a new db entry
Nikolay
I mean, you can try and generate that stuff based on e.g. bot api docs page but i don't think it's worth the time
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Nikolay
If the class you want to make into db entity has typehints you can try to generate entities based on these typehints, but that also sounds time consuming and very fragile
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Nikolay
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
yes..
Nikolay
I'm quite new to pony so i don't know if it has a method to convert nested dicts into linked entities
Henri
Hi! A short question. When should I use str and when LongStr? I gonna use PostgreSQL. To which datatypes are they translated?
Alexander
Hi! In PostgreSQL they are both translated to text, but LongStr is lazy loaded, so if you have some big text field which is seldom used you can defime it as LongStr to avoid loading this field each time when the object is loaded. In other databases LongStr is translated to CLOB datatype
Henri
Thanks for your quick reply!
Anonymous
If I have a row in memory and do stuff with it, and someone else changes a cell in the database, how would I go about reading the changed cell? (I know which cell and there are discrete points at which I check it). I tried doing a commit, followed by ModelClass[row_id], but I seem to still get the old value.
Nikolay
Nikolay
search docs for "volatile"
Anonymous
volatile is actually already set to True. Hmm...
Nikolay
well, I imagine using db_session as context manager and leaving then starting session again should 100% refresh everything
Nikolay
or perhaps it depends on whether you select specific attributes vs whole object
Nikolay
Anonymous
The field is marked volatile, the function has @db_session() and I do a commit before accessing the row again using the primary key.
(It would indeed be nested)
Anonymous
Perhaps reading just the single cell could work, I'll try that
Nikolay
well, reading pony docs it seems like volatile should do what you want so i'm out of ideas
Nikolay
Nikolay
Are you absolutely sure the value was changed and committed to the db?
Nikolay
What db provider and ponyorm version are you using? Not that I can do anything with this information but maybe someone else can
Anonymous
Fairly sure. I changed it with the mysql command using --batch, but I'll make extra sure
Anonymous
mariadb and pony==0.7.11
Anonymous
Alright, it definitely gets updated in the DB
Anonymous
Aha! Reading just the single value worked
Anonymous
Good enough for me, though I am still a bit mystified by this behaviour
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Hi, what's the right way to execute a regex query?
I tried with lambda,
orm.Rule.select(lambda rule: re.match(r"vvv", rule.text))
but I get an error like this:
AssertionError: flags (inside match)
Alexander
What database are you using?
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Postgres
Alexander
Pony doesn't know how to translate Python regex to PostgreSQL syntax. You need to use raw SQL fragments and write PostgreSQL expression
select(r for r in Rule if raw_sql("r.text ~ '^(a|b+)'"))
Alexander
If you use generator syntax then the name of the table alias will be the same as the name of the loop variable ("r" in this case)
If you use lambda syntax, as in orm.Rule.select(...) then the name of table alias will be equal to first letter of class name lowercased (for Rule it will be "r"), so you can write:
orm.Rule.select(lambda rule: orm.raw_sql("r.text ~ '^(a|b+)'"))
Jan
Hey I am using pony with sqlite in a gui application to save a session in a file (sqlite). I would like to implement a way to open another session in the application. I had the idea to call db.bind a second time to load another sqlite file but that does not seem to work
Jan
How could one implement this functionality?
Jim
You wanna run 2 sessions on the same db ? or 2 dfferents database ?
Alexander
If your GUI application uses threads, you can open two simultaneous db_session in two concurrent threads, and use the same db object and same entities in both db sessions
Anonymous
Does the same hold for coroutines / green threads?
Alexander
No, because coroutines share the same thread, and db_session is tied to thread. Transactions will not work correctly with coroutines
Alexander
Currently, if you write async code with coroutines, you can use pewee-async or Tortoise ORM
https://peewee-async.readthedocs.io/en/latest/
https://tortoise-orm.readthedocs.io/en/latest/
Nikolay
Let's say I have Event and Person models and I also have Ticket(id, event_id, person_id, some other info). In my particular case there's either 0 or 1 ticket per person per event so it's like many-to-many with extra information.
What I want to achieve is, given a person, list all events and for each event list the ticket related to that person if any. A list of event, ticket pairs where ticket might be None, or list of event, tickets pairs where tickets might be an empty list, or something equivalent .
If I was writing raw SQL this would be a left join with additional ON conditions, Event LEFT JOIN Ticket ON Ticket.event_id = Event.id and Ticket.user_id = ?, but some google results suggest pony can't do that.
Is it possible to do what I want with pony without making selects in a loop? What are my options?
Nikolay
Attempts to
select((e, e.tickets.filter(lambda e: e.user == user)) for e in Event)
select((e, e.tickets.filter(lambda e: e.user == user)[:]) for e in Event)
select((e, Ticket.get(event=e, user=user)) for e in Event)
result in rather cryptic errors about SQL AST or EntityMonad or not so cryptic TypeError.
Other things I tried result either in similar errors, N+1 selects, or fetching all tickets and filtering them by user on python side.
Anonymous
No, because coroutines share the same thread, and db_session is tied to thread. Transactions will not work correctly with coroutines
Well, I do use green threads with eventlet, but it's all in a single db_session. I was just asking out of curiosity.
I wouldn't recommend peewee, though. I used it before switching to pony and... well, there is a reason I switched. The interface is pretty nice, but there are some rather bad details.
(For example: There is a function to check if you are connected to the DB. If you lose connection to the DB between queries, it will still come back with True. Any subsequent queries will fail and it is up to you to re-establish the connection. Made a bug report about it and was told to f-off, essentially)
Henri
I think I've found a bug in the diagram editor.
When I have to date attributes in the same entity I get the following error when creating the SQL code:
Something went wrong: Incorrect type of attribute: ?.?
The model look like this:
python
class Test(db.Entity):
id = PrimaryKey(int, auto=True)
date = Optional(date)
expire = Optional(date)
Henri
Here is a link: https://editor.ponyorm.com/user/henri/test_dates/designer
Alexey
@wadera hi, seems to be working
Alexey
Henri
You have to go to on of the databases. E.g. SQLite.
Alexey
the name of the attribute cannot be date
Alexey
if you rename it, should be fine
Henri
That's true. But if there is only one attribute with the name date it works also...
Only if I add the second I get the problem.
Alexey
because the next one is using not date imported from datetime, but the previous attribute, which doesn't work
Henri
And both have to be date type.
Henri
Ok I see.
Henri
Thanks for clarification.
Lucky
Lucky
Nvm, date is from datetime, and properly imported.
Lucky
But just change it to
import datetime
and in the model declaration use
date = Optional(datetime.date)
Lucky
Assuming that SQL code generation is based on the server actually trying to execute that python model,
all the editor would need to do is to replace the import name with something randomly generated thing which is not part of the model variables.
E.g.:
from datetime import date as date_lib_1ef839ae5aa3
and
date = Optional(date_lib_1ef839ae5aa3)
Alexey
Nikolay
Is there a way to enable debug mode globally as opposed to per thread? The threads are created by a library, I don't exactly have control over their target.
Nikolay
I guess I do have some control, since all queries are in my code in callbacks passed to the library, but it's rather inconvenient to enable debug mode on basically every db_session rather than in one place. And number of places i'll need set debug mode in and number of times it will be (re)set to True during runtime is much greater than actual number of running threads, which makes this feel even more awkward.
Henri
Is it possible to use the postgreSQL ltree extension providing the ltree data type somehow together with PonyORM?