Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm SQLAlchemy autocomplete

I started evaluating PyCharm 3 professional edition because I will be working on several Pyramid + SQLAlchemy projects. One of the things I would really love to have is SQLAlchemy autocomplete.

I created a new starter project with the alchemy scaffold following these instructions. I also installed the SQLAlchemy package for the interpreter and virtual environment I am using for this project.

Also, when I created a new pycharm project for this code, the IDE suggested me to install the pyramid, sqlalchemy and other packages. Of course I accepted the suggestion and let the IDE install all of those packages.

In the models.py file, the DBSession is declared as follows:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

In the views.py file, the DBSession is used this way:

one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()

So I started playing with the IDE and did something like this: typed DBSession. and the IDE just gave me some few suggestions, within which the 'query' function was not listed. Then I tried typing: DBSession.query(MyModel). and pressed Ctrl+Space to try to get suggestions and a 'No suggestions' message showed up.

I would really like to have the SQLAlchemy suggestions of functions that I could use on my DBSession variable (like filter, filter_by, first, etc). I would say that this is mandatory for me :)

Is there something I am missing? Or, PyCharm doesn't support this?

like image 432
Roland Pish Avatar asked Feb 15 '14 04:02

Roland Pish


People also ask

How do I enable autocomplete in PyCharm?

Invoke basic completionPress Ctrl+Space or choose Code | Code Completion | Basic from the main menu. If necessary, press Ctrl+Space for the second time (or press Ctrl+Alt+Space ).

Why Autocomplete is not working in PyCharm?

You need to go into the project settings and configure the interpreter to point at the virtualenv. PyCharm will then index the interpreter and allow you to autocomplete. The virtualenv may be auto-detected in the dropdown menu on the left.

What does Sqlalchemy all () return?

As the documentation says, all() returns the result of the query as a list.


1 Answers

I use a type declaration after a variable assignment:

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

...

engine = create_engine(connect_str, max_overflow=10)
engine: Engine

As a use for variables in for loop, I used:

for table, meta in tables.items():
    meta: Table
    pass

in which, tables is sqlalchemy.orm.mapper.Mapper, and table is an imported type:

from sqlalchemy import create_engine, Table
like image 101
Fify Avatar answered Sep 21 '22 13:09

Fify