Anonymous
guys!
Anonymous
be able to transform the data
Anonymous
the data comes in hex format... so I used the Shapely lib to generate the MULTILINESTRING
Anonymous
thanks @Volbil
Christian
Volbil
Anonymous
yes, I consult the data and transform it to the format that the frontend needs...
Volbil
Anonymous
Huuumm ok
Anonymous
Thanks
Ben
Quick question: Is there a way for me to do read-only transactions not caring about the data changing while I do it nor locking the db? If I just do a rollback before the end of the transaction (having changed nothing) will it still trigger a optimisticcheck error?
Alexander
Can you remind me, what database do you use?
Ben
postgres
Alexander
If you change objects in the database, Pony will execute UPDATE before the next SELECT to ensure that new query results take all updates into account.
These updates can block parallel transactions which are trying to update the same rows, so it is better to avoid updates if, in the end, you plan to change nothing.
To prevent optimistic check errors, you can specify optimistic=False when creating the most-outer db_session.
Ben
and this wont create a lock?
Ben
also this was still giving me errors in case an object appeared in a collection
Ben
In this route im not changing a single thing
Alexander
Regarding objects appearing in collections I need to check, I had impression that db_session(optimistic=False) should not check this, but I may be wrong on that
Alexander
You can try the following at the very beginning of your db_session:
connection = db.get_connection()
connection.execute("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
Then you should not experience errors when parallel transaction change items in a collection. You will see the database state at the start of the transaction
Ben
Thanks a lot
Ben
Hi! What you suggested worked perfectly, thanks for that! One more question: is there a way for me to assign several items of a set via ID (rather than getting the object). i.e. can I do clients.bills = [1,2,3,4]?
Alexander
Try this:
bills = Bill._get_by_raw_pkval_((id,)) for id in id_list
client.bills = bills
But you need to be sure that the object with this id actually exists in the database
Ben
ah right, but still need to do a separate function call, is that faster than just doing bills = select(bill for bill if bill.id in id_list) ?
Alexander
It does not perform SELECT and just creates objects in memory, assuming they exist in the database
Ben
is it db_session dependant?
Alexander
yes
Ben
ok so can't do it in one db_session and reuse it in another
Alexander
You can data = pickle.dumps(object_list) in one db_session and object_list = pickle.loads(data) in another
Alexander
or you can make id_list in one db_session and get objects from id_list in abother
Ben
thanks!
Volbil
Something I wander what those bots trying to achieve by writing some random sentences. It looks very unnatural 🤔
Joonatan
I'm getting the error "pony.orm.core.TransactionError: An attempt to mix objects belonging to different transactions". I don't know how the queries are supposed to be made. Apparently the Indentity Map is still alive and I'm using possibly old objects. Am I supposed to commit somwhere?
Joonatan
Fixed it, I understood where the sessions were
Luigi
Hi all, two quick questions:
1) I've seen on github that the latest release of pony orm is of months ago, soon will be a year. It's the project live? There's something like a roadmap for example to support Pyton 3.10?
2) do you know an open source project in github that's based on pony orm for DB activities and can be used as a "real world" sample to see pattern of use and so on?
Than you & KR
Luigi Piatti
Volbil
Volbil
Not sure if there open source projects available for example but I use Pony as my main orm for almost a year and I can say that it's very fast, flexible and reliable
Volbil
Probably most pythonic orm I've seen
Volbil
I think Pony docs is perfect place to see examples and etc
Luigi
@Volbil I'm learnig python and I'm using Pony for orm for your same reason.
To know that the project isn't active is afterwards probably a reason to check if some other framework has a better value in the mid \ long therm- ..
Volbil
Honestly Pony is perfect orm
Volbil
Even if there is no active development
Alexander
Everything what we started to develop is being developed in another branches.
Such as migrations_dev and py_3_10_dev.
We have low resources and we develop in every free time window we have.
Pony is open source project, so if you want to help you may fork, implement and pull request your code.
Most critical thing now is py 3.10 support but i believe i could finish it in a near future.
migations_dev seem to be working for postgresql but with some bugs which should be fixed.
Overall we dont drop this project, but we have hard times to develop.
Volbil
Luigi
Lucky
How does pony handle restrictions in the column name length?
Alexander
If you specify max length for str attributes, like code = Required(str, max_len=20), then Pony will create a column with a type VARCHAR(20), and on attribute assignment it will check that the provided string has correct length.
If an attribute is part of the primary key, it will work the same way.
In related objects Pony uses the same type for reference columns.
For numeric columns, in PostgreSQL if a primary key has type SERIAL the reference column in linked table will have the INTEGER type, and for BIGSERIAL primary key the reference column type will be BIGINT
Genesis Shards Community
Ok
A
Hi all. I'm using SQLite. Is there a way Pony can create a "check" constraint? In plain SQL, something like this:
CREATE TABLE tshirt(color not null check(color in ('red','green','blue')))
I've tried using an Enum but the check constraint isn't present in the created database. I can check it in code using the Enum, but I'd really like it in the database.
Example code here: https://gist.github.com/austinjp/d2751400f056fb2b8400dbee0b2f32cc
I've hunted Google but not found anything yet. The Pony docs only seem to mention foreign key constraints: https://docs.ponyorm.org/search.html?q=check+constraint
Can it be done?
A
Hmm that link looks broken. Here it is again:
https://gist.github.com/austinjp/d2751400f056fb2b8400dbee0b2f32cc
Alexander
Hi, you can specify `sql_type`option for the attribute and add check constraint as a part of a type definition:
class Tshirt(db.Entity):
color = Required(str, sql_type="text not null check(color in ('red','green','blue'))")
A
Amazing. Thanks for the quick reply, Alexander.
A
Really enjoying Pony. Thanks for all your work.
A
@metaprogrammer I noticed that Pony seems to convert everything in sql_type to uppercase including the values "red", "green" and "blue". This may be a problem for me. Is there a way to prevent this and retain the case of the values?
The schema of the example you gave looks like this:
CREATE TABLE IF NOT EXISTS "tshirt" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"color" TEXT NOT NULL CHECK(COLOR IN ('RED','GREEN','BLUE')) NOT NULL
);
For now, I have added COLLATE NOCASE to the sql_type definition as a workaround.
Alexander
Unfortunately it is not possible to turn of capitalization of sql_type right now, we will change that
Alexander
A
Interesting, thanks, I'll check that out 👍
A
@metaprogrammer Overriding DBSchema.case works, many thanks again.
Mario
Hey guys, is there any reason to delete the exc variable in the finally clause?
Alexander
Not deleting the exc variable can create a memory leak, because exc keeps the reference to the traceback, traceback keeps the reference to the current frame, and the current frame keeps the reference to the exc object
Alexander
https://www.python.org/dev/peps/pep-3134/#open-issue-garbage-collection
Mario
Ok, thanks! I'll check that out!
Jeff
Anyone here have experience making graphql apis with pony ORM?
Jeff
I am looking into http://strawberry.rocks and im led to believe it works well with Pony because it has pydantic support
Andrey
Alexander
Current version of Pony does not support Python 3.10
We will release a new version soon with 3.10 support
Henri
Christian
Also recommends GraphQL: https://fastapi.tiangolo.com/advanced/graphql/
Christian
Be aware that the current version of PonyORM wasn't designed with async in mind. So if that's a requirement, you might want to use a different ORM.
Mario
Hey guys, what is the best way to make a query that filters by se condition in a fk-related entity, but only if it exists. For instance: persons.filter(lambda person: person.dog.name == "bob"). In that case all persons without dod won't match the query. How should I modify it to include them? Why persons.filter(lambda person: not person.dog or person.dog.name == "bob") doesn't work?
Alexander
Can you explain what you want to achieve? I did not understand your goal
A
Are you looking for this?
https://docs.ponyorm.org/
A
Hi all.
I noticed (after a lot of head-scratching!) that Pony seems to use the "shorthand" foreign key declaration in SQLite, instead of an explicit "foreign key (...)" clause.
Demo here:
https://gist.github.com/austinjp/06dae04e2705a5a898c072007d4f0fbd
Is that deliberate? I'd expected to see "foreign key (...)" in the schema dump, and was confused when it wasn't there, resulting in me spending several hours "debugging" unnecessarily.
My preference is for explicit rather than implicit. Is there any way I can get a "foreign key (...)" clause in the schema?
It's not a major problem, though.
Thanks! 😀
Alexander
Hi! It is not officially supported now, but you can try the following at the beginning of your program:
from pony.orm.dbproviders.sqlite import SQLiteSchema
SQLiteSchema.named_foreign_keys = True
A
A
@metaprogrammer I'm getting an error with that if I add it at the top of my example script. Does SQLiteSchema need to be insantiated?
File "/whatever/venv/lib/python3.8/site-packages/pony/orm/dbschema.py", line 381, in exists
return provider.fk_exists(connection, foreign_key.child_table.name, foreign_key.name, case_sensitive)
TypeError: fk_exists() takes 4 positional arguments but 5 were given.
A
Python 3.8.10, Pony 0.7.14
Alexander
Ok, so it turns out more complex than I hope :)
Right now it is not supporting, I think in the future we should switch to named foreign keys for all databases