Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlalchemy example doesn't save rows #247

Open
BlinkyStitt opened this issue Feb 1, 2018 · 3 comments
Open

sqlalchemy example doesn't save rows #247

BlinkyStitt opened this issue Feb 1, 2018 · 3 comments

Comments

@BlinkyStitt
Copy link
Contributor

BlinkyStitt commented Feb 1, 2018

I was following https://github.com/aio-libs/aiomysql/blob/master/docs/sa.rst and I noticed that running the example multiple times did not work as expected.

The example:

import asyncio
import sqlalchemy as sa

from aiomysql.sa import create_engine


metadata = sa.MetaData()

tbl = sa.Table('tbl', metadata,
               sa.Column('id', sa.Integer, primary_key=True),
               sa.Column('val', sa.String(255)))


@asyncio.coroutine
def go():
    engine = yield from create_engine(user='root',
                                      db='dabbot',
                                      host='db',
                                      password='dabbot')

    with (yield from engine) as conn:
        yield from conn.execute(tbl.insert().values(val='abc'))

        res = yield from conn.execute(tbl.select())
        for row in res:
            print(row.id, row.val)

asyncio.get_event_loop().run_until_complete(go())

bash-4.4$ python /app/simple.py 
1 abc
bash-4.4$ python /app/simple.py 
2 abc
bash-4.4$ python /app/simple.py 
3 abc
bash-4.4$ python /app/simple.py 
4 abc
bash-4.4$ python /app/simple.py 
5 abc

Then I opened up adminer and added 6 def

bash-4.4$ python /app/simple.py 
6 def
7 abc
bash-4.4$ python /app/simple.py 
6 def
8 abc

I think the example is missing conn.execute('commit').

The example at https://github.com/aio-libs/aiomysql/blob/master/examples/example_simple_sa.py has the same issue. I removed the "drop" statement and it doesn't persist rows like I expect.

bash-4.4$ python /app/simple.py 
1 abc
2 xyz
bash-4.4$ python /app/simple.py 
3 abc
4 xyz
bash-4.4$ python /app/simple.py 
5 abc
6 xyz
@BlinkyStitt
Copy link
Contributor Author

#224 looks like the same issue. The examples should probably make a note about autocommit.

@jettify
Copy link
Member

jettify commented Feb 20, 2018

Good point. Would you like to make PR?

@BlinkyStitt
Copy link
Contributor Author

BlinkyStitt commented Mar 9, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants