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

docs: sqlalchemy pattern document has an invalid import #1978

Closed
sc68cal opened this issue Aug 4, 2016 · 9 comments
Closed

docs: sqlalchemy pattern document has an invalid import #1978

sc68cal opened this issue Aug 4, 2016 · 9 comments

Comments

@sc68cal
Copy link

sc68cal commented Aug 4, 2016

The declarative section includes the following example:

Declarative
-----------

The declarative extension in SQLAlchemy is the most recent method of using
SQLAlchemy.  It allows you to define tables and models in one go, similar
to how Django works.  In addition to the following text I recommend the
official documentation on the `declarative`_ extension.

Here's the example :file:`database.py` module for your application::

    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session, sessionmaker
    from sqlalchemy.ext.declarative import declarative_base

    engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))

db_session is a variable, and a paragraph below, it tries to import it. Which isn't really possible.


    from yourapplication.database import db_session

    @app.teardown_appcontext
    def shutdown_session(exception=None):
        db_session.remove()

This documentation is misleading

@wgwz
Copy link
Contributor

wgwz commented Aug 5, 2016

@sc68cal I don't think that there is anything wrong with that import in principle. But of course it might be nice for the application structure to be stated more explicitly.

@sc68cal
Copy link
Author

sc68cal commented Aug 5, 2016

Where is there a function named db_session? There is a variable named db_session but you can't import variables....

@ThiefMaster
Copy link
Member

ThiefMaster commented Aug 5, 2016 via email

@wgwz
Copy link
Contributor

wgwz commented Aug 7, 2016

@sc68cal What do you think? If things make sense now, you should maybe go ahead and close the issue.

@davidism
Copy link
Member

davidism commented Aug 8, 2016

I don't see anything wrong with this import.

@davidism davidism closed this as completed Aug 8, 2016
@sc68cal
Copy link
Author

sc68cal commented Aug 8, 2016

That's a post of me trying to follow the documentation, albiet with a module named api instead of database like the instructions has.

There is no db_session.py that the docs includes. The docs talk about a database.py module. So if anything it should be

from yourapplication.database import database

And I guess it would be database.db_session.close() ?

The issue is the docs are quite ambiguous.

@ThiefMaster
Copy link
Member

You can import anything (variable, functions, etc.) from a module...

@davidism
Copy link
Member

davidism commented Aug 8, 2016

I can't reproduce your issue. I created a database.py file based on the docs then imported from it successfully in other modules.

This is not an issue with Flask or its docs, it appears to be an issue with your understanding of Python imports. The Flask issue tracker is not the place to resolve that.

If you have a question about your own code, please post on Stack Overflow after creating a minimal, complete, and verifiable example.

@sc68cal
Copy link
Author

sc68cal commented Aug 8, 2016

The issue was there was a error in an import further down the stack. Basically PEBCAK, sorry for the waste of time

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants