1

My code is simple:

app = Flask(__name__)
app.config.from_object('config')

db = SQLAlchemy(app)
db.metadata.reflect()

And it throws no errors. However, when I inspect the metadata after this reflection, it returns an empty immutabledict object.

The parameters in my connection string is 100% correct and the code works with non-RDS databases.

It seems to happen to others as well but I can't find a solution.

Also, I have tried to limit the reflection to specific tables using the "only" parameter in the metadata.reflect function, and this is the error I get:

sqlalchemy.exc.InvalidRequestError: Could not reflect: requested table(s) not available in mssql+pyodbc://{connection_string}: (users)

3 Answers 3

1

I've fixed it. The reflect() method of the SQLAlchemy class has a parameter named 'schema'. Setting this parameter, to "dbo" in my case, solved it.

I am using Flask-SQLAlchemy, which does not have the said parameter in its reflect() method. You can follow this post to gain access to that parameter and others, such as 'only'.

Sign up to request clarification or add additional context in comments.

Comments

0

This error occurs when reflect is called without the schema name provided. For example, this will cause the error to happen:

metadata.reflect(only = [tableName])

It needs to be updated to use the schema of the table you are trying to reflect over like this:

metadata.reflect(schema=schemaName, only = [tableName])

Comments

0

You have to set schema='dbo' in parameter for reflect.

db.Model.metadata.reflect(bind=engine, schema='dbo', only=['User'])

and then create model of your table:

class User(db.Model):
    __table__ = Base.metadata.tables['dbo.User']

and to access data from that table:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.