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

How to support reflect with a dict of table names and database views #32

Closed
gsvitak opened this issue Sep 15, 2016 · 6 comments
Closed

Comments

@gsvitak
Copy link

gsvitak commented Sep 15, 2016

Hello

Thanks for the work. It makes integrating SqlAlchemy into a custom framework a snap.

I have an existing database with some database views. I want to reflect the database to map the tables and the views in a dictionary.

  1. How would I support only reflecting a select set of tables?
  2. How would I support reflecting views? I noticed the reflect method only support tables in the metadata?

Would I follow the Flask pattern and subclass the Manager class and override the needed methods?

Thanks
Greg

@dgilland
Copy link
Owner

It looks like SQLAlchemy's reflect() method supports an only argument that can be used to limit the reflection to subset of table names. However, alchy doesn't expose that option in the Manager.reflect() method (but obviously you can subclass it to extend that behavior).

For view, SQLAlchemy has a way to reflect views: http://docs.sqlalchemy.org/en/latest/core/reflection.html#reflecting-views

@gsvitak
Copy link
Author

gsvitak commented Sep 15, 2016

Thanks for the info. Can you please provide an example of how to use the only parameter?

I only have 1 database and when I pass a dictionary to the method of the table names, I get a message stating that the mapping is not available in SQLALCHEMY_BINDS object.

Thanks,
G

@dgilland
Copy link
Owner

What's your code that produces the error?

@gsvitak
Copy link
Author

gsvitak commented Sep 15, 2016

Thanks for the offer to help.

class Base(ModelBase):
    # extend/override ModelBase if necessary
    pass

Model = make_declarative_base(Base=Base)

config = {
    'SQLALCHEMY_DATABASE_URI': conn_string,
}

reflect_tables = ['people']
db = Manager(config=config, Model=Model)
db.reflect(reflect_tables)

@dgilland
Copy link
Owner

alchy.Manager.reflect doesn't support the only argument. That's something that's supported in SQLAlchemy's reflect() method but alchy doesn't curry that argument to it unfortunately. So you would need to extend Manager:

@gsvitak
Copy link
Author

gsvitak commented Sep 15, 2016

Oh.. stupid on my part.. I misread your previous response. Thanks.

Greg

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

No branches or pull requests

2 participants