I am new to SQLAlchemy. I am trying to make it work with a MariaDB. I am trying to get all the records from the DB and cant seem to get the correct syntax for this.
I have tried Employee.query.all(), and other variations and none seem to work for me. I either get an error with 'all' attribute or with the the 'query' attribute
all_employee = session.query.all(Employee)
AttributeError: 'function' object has no attribute 'all'
############################# CODE ##############################
# Define the MariaDB engine using MariaDB Connector/Python
engine = sqlalchemy.create_engine(f"mariadb+pymysql://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB}")
Base = declarative_base()
class Employee(Base):
__tablename__ = 'employees'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
firstname = sqlalchemy.Column(sqlalchemy.String(length=100))
lastname = sqlalchemy.Column(sqlalchemy.String(length=100))
active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
class EmployeeSchema(ma.Schema):
class Meta:
#all fields from DB
fields = ('id', 'firstname', 'lastname', 'active')
#creates db model
Base.metadata.create_all(engine)
#initialize Session
Session = sqlalchemy.orm.sessionmaker()
Session.configure(bind=engine)
session = Session()
employee_schema = EmployeeSchema()
#Get Employee
@app.route('/', methods=['GET'])
def get_employee():
all_employee = session.query.all(Employee)
app.logger.info(f"employees is {str(all_employee)}")
result = employee_schema.dump(all_employee)
return jsonify(result)