hipertracker
Does Pony support database migrations?
Alexander
Not yet, but we are working on it. Migrations for PostgreSQL will be available really soon
hipertracker
I can see it has an integration with Flask. Will it has the integration with FastAPI? It’s must faster than Flask and is async.
Alexander
You can use Pony with FastAPI with one caveat: you should never call async functions (with await) inside db_session
hipertracker
OK. I can use async for FastAPI controller and inside it can run with db_session: without-async-code-here
hipertracker
It looks like db migrations is the main missing part at the moment.
Alexander
yes
Christian
Hi, is it possible to "stop" (rollback?) a single new instance during a db_session transaction and keeping the others? This code below doesn't seem to work, because new_game is committed to the database anyhow:
for item in request_objects:
try:
new_game = Game()
raise ValueError('You shall not pass!')
except ValueError:
del new_game
continue
Christian
Nevermind, it works with the regular delete() method: new_game.delete()
Christian
There's a tiny spelling mistake in pony/core.py:4569 and :4582: Change attriute to attribute.
Alexander
Thanks!
Andrey
How can I update a record?
Alexander
Select it and change values.
Alexander
foo = Foo.get(attr1=...)
foo.bar = 1
foo.baz = 'asd'
Andrey
Andrey
thanks you
Alexander
Not really. Pony understands where to commit
Andrey
Andrey
Thank you for the answer
Andrey
Andrey
What have I done wrong? (
Matthew
You need all code that uses pony objects inside a database session
Matthew
Put that code within “with orm.db_session:” block
Andrey
Andrey
Matthew
You need all of the code that deals with pony objects in that one with block
Andrey
Andrey
and how
Matthew
Myobj.to_dict() then dump to JSON like any other dict
Andrey
Maik
hey guys,
i have a question about ponyorm with mysql i try to add a name with a emoji in it and i have set the db to varchar and i get this error
pony.orm.core.UnexpectedError: Object Group[new:1] cannot be stored in the database. DataError: 1366 Incorrect string value: '\xF0\x9F\x94\x97 I...' for column 'name' at row 1
does anyone now how to ad a emoji to the db?
Matthew
you need to change the mysql charset https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-mysql-database
Maik
i tryed this but still same error
Matthew
does trying to insert using a raw sql query work?
Maik
Matthew
try it
Matthew
if you still have the error, you probably didn't change the collation correctly
Maik
Christian
https://docs.ponyorm.org/database.html#using-database-object-for-raw-sql-queries
Christian
Or here, more specifically: https://docs.ponyorm.org/api_reference.html#Database.execute
Maik
found the issue.. it was not mysql it was ponyorm issue, i needed to add to db.bind the charset='utf8mb4'
Maik
this did the trick
Christian
Yay! :)
Maik
thansk for the support
Maik
i try to update a value in the db, this value is edited only on 1 place in the code... why i get this error?
pony.orm.core.UnrepeatableReadError: Value of User.referred_user for User[16] was updated outside of current transaction (was: None, now: User[2])
Maik
Maik
it referred to the same table
Alexander
This means the following:
1) you started db_session. In this db_session you read User[16].referred_user (it was None at this moment).
2) some other transaction modified the value of User[16].referred_user
3) you updated some attribute of User[16]. At the end of db_session Pony tries to save the changes to the database, but for additional safety it checks that at this moment all fields of User[16] still have the same values as in the beginning of db_session. Pony sees that the value of User[16].referred_user was suddenly changed by other transaction from None to User[2]. As you didn't read this new value User[2] in current db_session it may be dangerous to save changes, as it may overwrite previous update. So Pony throws an exception
Maik
1 - yes i read it and it was none thats correct
2 - there is only one modified place
3 - and yes i try to update the value for None to User[2]
Maik
the group works fine only the user not
Alexander
It looks like the object was modified by some concurrent transaction at the same time
Alexander
Or you used raw SQL
Maik
Maik
Maik
this is the whole code
Alexander
asyncio.run call does not look correct to me
Maik
Maik
Maik
i disabled it to be sure.... but still not working
Alexander
Use User.get_for_update instead of User.get
Maik
@metaprogrammer same error
Maik
Maik
i just notice is only for a few users
Maik
ow now if works...
Maik
was it a memory issue?
Alexander
It should not be a memory issue
Maik
if i empty the db manual to test again it gives the same error again
Alexander
if not user.userid == user
This check will always be True, user.userid is a number, while user is an entity instance
Maik
Alexander
Probably not. I see the following possible reasons:
1) The code run in several threads or processes, and another thread/process already updated the object in the middle of db_session (but get_for_update should prevent this scenario)
2) You have some async-related error in the code, like performing async call (with await) inside db_session
3) You perform database update which Pony does not aware about, using raw SQL update or, say, a database trigger
Alexander
You can use db_session(optimistic=False) to prevent Pony from detection of this type of errors, but it may just mask the real problem
Maik
Volbil
Hey @metaprogrammer recently I been stuck with following issue pony.orm.core.UnrepeatableReadError: Value of Balance.balance for Balance[171] was updated outside of current transaction (was: Decimal('50001.605299999996'), now: Decimal('50001.60530000'))
Problem is, I have only one transaction in code. I assumed that it happened because I was get balance object twice inside this transaction but after caching it one after it got fetched this error keeps popping up. Any idea what could be the cause?
Sql debug: https://paste.ubuntu.com/p/r4hy2G5QdV/
Full error traceback: https://paste.ubuntu.com/p/9nbYpMFvxW/
Volbil
Balance and address models looks like this:
class Address(db.Entity):
_table_ = "addresses"
address = orm.Required(str, index=True)
transactions = orm.Set(
"Transaction", table="address_transactions",
reverse="addresses"
)
balances = orm.Set("Balance")
class Balance(db.Entity):
_table_ = "address_balances"
balance = orm.Required(Decimal, precision=20, scale=8, default=0)
address = orm.Required("Address")
currency = orm.Required(str)
orm.composite_index(address, currency)
Alexander
Hi, is your application synchronous or asynchronous?
Volbil
It's synchronous
Alexander
Does it use threads?
Volbil
Nope