tessl install tessl/pypi-sqlmodel@0.0.0SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
Agent Success
Agent success rate when using this tile
85%
Improvement
Agent success rate improvement when using this tile compared to baseline
1x
Baseline
Agent success rate without this tile
85%
Create an employee management system that uses polymorphic inheritance to model different types of employees. The system should store all employees in a single database table with a discriminator field to identify the specific employee type.
Create a base Employee model with common attributes:
id: Auto-incrementing integer primary keyname: String field for the employee's nameemail: String field for the employee's email addresssalary: Float field for the base salaryCreate two specialized employee types that inherit from the base Employee model:
Engineer: Adds a programming_language string fieldManager: Adds an department string fieldImplement single-table inheritance where all employee types are stored in the same database table using a discriminator column to identify the type.
Create a database with SQLite, initialize the tables, and implement the following operations:
Print the results showing that the polymorphic queries work correctly, displaying each employee's type-specific attributes.
# test_employee.py
from employee import Employee, Engineer, Manager, engine
from sqlmodel import Session, select
def test_create_and_query_engineers():
with Session(engine) as session:
# Create engineers
eng1 = Engineer(name="Alice Smith", email="alice@example.com", salary=95000.0, programming_language="Python")
eng2 = Engineer(name="Bob Jones", email="bob@example.com", salary=98000.0, programming_language="Go")
session.add(eng1)
session.add(eng2)
session.commit()
# Query engineers
statement = select(Engineer)
engineers = session.exec(statement).all()
assert len(engineers) >= 2
assert all(isinstance(e, Engineer) for e in engineers)
assert any(e.programming_language == "Python" for e in engineers)# test_employee.py
from employee import Employee, Engineer, Manager, engine
from sqlmodel import Session, select
def test_create_and_query_managers():
with Session(engine) as session:
# Create managers
mgr1 = Manager(name="Carol White", email="carol@example.com", salary=110000.0, department="Engineering")
mgr2 = Manager(name="David Brown", email="david@example.com", salary=105000.0, department="Sales")
session.add(mgr1)
session.add(mgr2)
session.commit()
# Query managers
statement = select(Manager)
managers = session.exec(statement).all()
assert len(managers) >= 2
assert all(isinstance(m, Manager) for m in managers)
assert any(m.department == "Engineering" for m in managers)# test_employee.py
from employee import Employee, Engineer, Manager, engine
from sqlmodel import Session, select
def test_query_all_employees():
with Session(engine) as session:
# Query all employees
statement = select(Employee)
all_employees = session.exec(statement).all()
assert len(all_employees) >= 4
# Verify we have both types
engineers = [e for e in all_employees if isinstance(e, Engineer)]
managers = [e for e in all_employees if isinstance(e, Manager)]
assert len(engineers) >= 2
assert len(managers) >= 2Your program should produce output similar to:
Engineers:
- Alice Smith (alice@example.com) - Salary: $95000.00 - Language: Python
- Bob Jones (bob@example.com) - Salary: $98000.00 - Language: Go
Managers:
- Carol White (carol@example.com) - Salary: $110000.00 - Department: Engineering
- David Brown (david@example.com) - Salary: $105000.00 - Department: Sales
All Employees:
- Engineer: Alice Smith (alice@example.com)
- Engineer: Bob Jones (bob@example.com)
- Manager: Carol White (carol@example.com)
- Manager: David Brown (david@example.com)Main implementation file containing the Employee, Engineer, and Manager models, database setup, and demonstration code.
Provides ORM and data validation support for SQL databases.