1

I have 'device' table in my database, I am trying to fetch the that device object so i can update some record to that table . I have tried meta = MetaData(bind=engine, reflect=True) its working fine but showing some warning "SADeprecationWarning: The MetaData.reflect flag is deprecated and will be removed in a future release. Please use the MetaData.reflect() method." When i am trying with MetaData.reflect() not getting the expected output .

from sqlalchemy import create_engine, inspect
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine.url import URL
from sqlalchemy import MetaData

db_url = {
         'drivername': 'postgres',
          'username': 'postgres',
          'password': '***',
          'host': 'localhost',
        'database': "test_db",
          'port': 5432}
engine = create_engine(URL(**db_url))
session_obj = sessionmaker(bind=engine)
meta = MetaData().reflect(bind=engine)
print(meta)
user_t = meta.tables['device']
sel_st = user_t.select()
conn = engine.connect()
res = conn.execute(sel_st)
for _row in res:
    print(_row)

Let me know what i am missing .

2
  • 2
    meta = MetaData(), then meta.reflect(bind=engine). Now you bind the return value of reflect(), which is probably None. It is common in Python that methods that mutate the object return None: stackoverflow.com/questions/11205254/… Commented Mar 4, 2020 at 14:03
  • @IljaEverilä yeah that's correct. But do you know how to implement the above scenario ? Commented Mar 5, 2020 at 5:03

1 Answer 1

2

change this line

meta = MetaData().reflect(bind=engine)
print(meta)
user_t = meta.tables['device']

into

meta = MetaData()
meta.reflect(bind=engine)
user_t= meta.tables.get('device')
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to Stackoverflow! When answering a question, try to explain what you are trying to achieve in your answer. Code only answers do not help the OP as much as code answers with explanations because the OP understands why they must do what you have done.

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.