Ask Your Question
1

What is the method to use bracket notation to retrieve a column from a table in declarative SQLAlchemy 2.0?

asked 2021-12-16 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-11-13 07:00:00 +0000

ladyg gravatar image

To retrieve a column from a table in declarative SQLAlchemy 2.0 using bracket notation, you can define the table as a class and then access the column using the getattr() method.

For example, if you have a table named "users" with a column named "id", you can define the table class as follows:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)

To retrieve the "id" column from the "users" table using bracket notation, you can do:

user = User()
id_column = getattr(User, 'id')

Alternatively, you can access the column directly using the class attribute syntax:

id_column = User.id
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-16 11:00:00 +0000

Seen: 14 times

Last updated: Nov 13 '22