I created declarative table.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String
from sqlalchemy.dialects.postgresql import UUID
import uuidBase = declarative_base()class User(Base):__tablename__ = 'users'id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True)name = Column(String)
I need to filter data. In the Flask-SQLAlchemy
, I do
name = 'foo'
User.query.filter_by(name=name).first()
But if I use SQLAlchemy
without Flask, then I get the error:
type object 'User' has no attribute 'query'
The only way that works for me is to filter the data through the session
.
engine = create_engine('DATABASE_URL')
Session = sessionmaker(bind=engine)
session = Session()name = 'foo'
user = session.query(User).filter_by(name=name).first()session.close()