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

Provide documentation for using reflect #705

Open
cowgill opened this issue Mar 21, 2019 · 4 comments
Open

Provide documentation for using reflect #705

cowgill opened this issue Mar 21, 2019 · 4 comments
Labels

Comments

@cowgill
Copy link

@cowgill cowgill commented Mar 21, 2019

I've searched high and low trying to find a flask-sqlalchemy reflect example that actually works.

There seem to be others with the same issue (via stack overflow) who are frustrated. I'm sure you're tired of having to troubleshoot for users as well. :-(

I'd be happy to write up the doc if you could provide a working example (using an app factory and without).

Here's what I've come up with thus far only to be hit with this error:

sqlalchemy.exc.ArgumentError: Mapper Mapper|Feed|autofeeds could not assemble any primary key columns for mapped table 'autofeeds

app.py

from flask import Flask
from src import views
from src.extensions import db

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_name)

    register_extensions(app)
    register_blueprints(app)
    return app

def register_extensions(app):
    db.init_app(app)
    db.reflect(app=app)
    db.Model.metadata.reflect(db.engine)
    return None

src/extensions.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

src/models.py

from .extensions import db

class Feed(db.Model):
    __tablename__ = 'autofeeds'
@rsyring
Copy link
Collaborator

@rsyring rsyring commented Apr 18, 2019

Soooo...what are you looking for here? You want Feed to have it's columns implicitly populated using SQLAlchemy's reflect functionality?'

In that case, I believe AutoMap is what you want and specifically automap_base.

I've not tried this code, but it might be as simple as:

AutoMapModel = automap_base(db.Model)
class Feed(AutoMapModel):
    __tablename__ = 'autofeeds'

AutoMapModel.prepare(db.engine, reflect=True)

Let me know. If it works or you can give me the version that works, I don't see any reason why we can't add it to the docs.

@davidism
Copy link
Member

@davidism davidism commented Apr 18, 2019

We generally don't add docs for things that are documented by SQLAlchemy unless they have different behavior. Is there something specific to Flask-SQLAlchemy here? We do provide db.reflect(), which is a shortcut for calling db.Model.metadata.reflect() for each bind, but there's no added behavior.

@rsyring rsyring added this to the 2.x milestone Apr 18, 2019
@rsyring
Copy link
Collaborator

@rsyring rsyring commented Apr 18, 2019

I think the thing that is causing confusion, and may be worth documenting, is that unlike traditional SA, we "hide" the declarative_base() work in SQLAlchemy.make_declarative_base().

With pure SA, you'd swap your usage of declarative_base() with automap_base(). But since we "hide" that from the user, it's not exactly clear how you'd use FSA with automap.

@davidism
Copy link
Member

@davidism davidism commented Apr 18, 2019

We already have some documentation on the metaclass: http://flask-sqlalchemy.pocoo.org/2.3/customizing/#model-metaclass. We also document make_declarative_base as part of the public API to override: http://flask-sqlalchemy.pocoo.org/2.3/api/#flask_sqlalchemy.SQLAlchemy.make_declarative_base. Based on the SQLAlchemy docs: https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html#using-reflection-with-declarative, I think the only thing wrong with the code in the op is that it needs to set __table__ to a reflected table.

@davidism davidism added the docs label Apr 18, 2019
@davidism davidism removed this from the 2.x milestone May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.