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

Cannot compile DropTable and CreateTable queries #93

Closed
abogushov opened this issue Sep 6, 2018 · 5 comments
Closed

Cannot compile DropTable and CreateTable queries #93

abogushov opened this issue Sep 6, 2018 · 5 comments

Comments

@abogushov
Copy link

Hello!

I try to create table via this code:

import asyncio

from asyncpgsa import pg
import sqlalchemy as sa
from sqlalchemy.sql.ddl import CreateTable, DropTable

users = sa.Table(
    'users', sa.MetaData(),
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('name', sa.VARCHAR(255)),
)


async def main():
    await pg.init('postgresql://localhost/test')
    await pg.query(DropTable(users))
    await pg.query(CreateTable(users))


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

and got an error from asyncpgsa

Traceback (most recent call last):
  File "/project/main.py", line 20, in <module>
    loop.run_until_complete(main())
  File "/project/.venv/python3.6/asyncio/base_events.py", line 467, in run_until_complete
    return future.result()
  File "/project/main.py", line 15, in main
    await pg.query(DropTable(users))
  File "/project/.venv/lib/python3.6/site-packages/asyncpgsa/pgsingleton.py", line 65, in query
    compiled_q, compiled_args = compile_query(query)
  File "/project/.venv/lib/python3.6/site-packages/asyncpgsa/connection.py", line 60, in compile_query
    compiled_params = sorted(compiled.params.items())
AttributeError: 'NoneType' object has no attribute 'items'

Same code using sqlalchemy works fine:

import sqlalchemy as sa
from sqlalchemy.sql.ddl import CreateTable, DropTable

users = sa.Table(
    'users', sa.MetaData(),
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('name', sa.VARCHAR(255)),
)

engine = sa.create_engine('postgresql://localhost/test')
engine.execute(DropTable(users))
engine.execute(CreateTable(users))

Thanks!

@nhumrich
Copy link
Contributor

nhumrich commented Sep 11, 2018

I dont run migrations in asyncio. So this has never been supported. This repo doesn't currently support everything sqlalchemy supports, only a subset. It looks like DropTable isnt "compiled", but rather an orm feature.
I would accept a PR to support this, but since its a feature I dont use, I dont have the time to work on this myself.

@denisdubovitskiy
Copy link
Contributor

denisdubovitskiy commented Oct 3, 2018

So, any ideas about how to create and drop tables between test cases? Is it really possible or we need to apply migrations to the database first?

The only solution I know is to manually iterate over the list of tables and create/drop them compiling the statements:

DropTable(table).compile(dialect=postgresql.dialect())

@nhumrich
Copy link
Contributor

nhumrich commented Oct 4, 2018

So, looking at this error a second time, it looks like the issue might be that droptable compiled doesn't have any params. compiled.params == None. So, maybe this is a fixable bug. Might be worth adding a none check to connection.py line 60. Would you have time to take a look at this, and maybe add a test case for it?

@denisdubovitskiy
Copy link
Contributor

denisdubovitskiy commented Oct 4, 2018

@nhumrich yeah, I've taken a closer look to the code of connection.py and decided to create my first GitHub PR ;-) #94
Please take a look at that

This PR is related to the following issue #59

@nhumrich
Copy link
Contributor

nhumrich commented Oct 5, 2018

This has been merged and is in version 0.25.0

@nhumrich nhumrich closed this as completed Oct 5, 2018
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