Created
September 9, 2018 23:55
-
-
Save amireldor/3c7f1701761d6c92855b868e5df07af0 to your computer and use it in GitHub Desktop.
I have some bug with pony ORM, I'll file a new issue I think
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a problem with this code with Python 3.6.5, pony 0.7.6 | |
# An exception is thrown: | |
# "pony.orm.core.TransactionError: db_session is required when working with the database" | |
# in the "I shall feed thee!" line. | |
# also see https://github.com/ponyorm/pony/issues/126 | |
import asyncio | |
from pony.orm import commit, db_session, select, Database, Required | |
db = Database() | |
db.bind(provider='sqlite', filename=':memory:', create_db=True) | |
class Cat(db.Entity): | |
name = Required(str) | |
age = Required(int) | |
db.generate_mapping(create_tables=True) | |
with db_session: # this is good | |
meow = Cat(name="meow", age=31) | |
purr = Cat(name="purr", age=14) | |
commit() | |
print("hoho here they are!", list(select(cat.name for cat in Cat))) | |
@db_session | |
async def feed_cats(): # this is bad | |
print("I shall feed thee!", list(select(cat.name for cat in Cat))) | |
await asyncio.sleep(2) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(feed_cats()) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment