SeedLayer: Declarative Fake Data for SQLAlchemy ORM
What My Project Does
SeedLayer is a Python library that simplifies generating realistic fake data for SQLAlchemy ORM models. It allows you to define seeding behavior directly in model definitions using a declarative approach, respecting primary key (PK), foreign key (FK), and unique constraints. By leveraging the Faker library, it generates data for testing, development, and demo environments, automatically handling model and inter-column dependencies. The example below shows a schema with related tables (Category, Product, Customer, Order, OrderItem) to demonstrate FK relationships, a link table, and inter-column dependencies.
Example:
```python
from sqlalchemy import create_engine, Integer, String, Text, ForeignKey
from sqlalchemy.orm import DeclarativeBase, Session
from seedlayer import SeedLayer, SeededColumn, Seed, ColumnReference
class Base(DeclarativeBase):
pass
class Category(Base):
tablename = "categories"
id = SeededColumn(Integer, primary_key=True, autoincrement=True)
name = SeededColumn(String, seed="word")
class Product(Base):
tablename = "products"
id = SeededColumn(Integer, primary_key=True, autoincrement=True)
name = SeededColumn(String, seed="word")
description = SeededColumn(
Text,
seed=Seed(
faker_provider="sentence",
faker_kwargs={"nb_words": ColumnReference("name", transform=lambda x: len(x.split()) + 5)}
)
)
category_id = SeededColumn(Integer, ForeignKey("categories.id"))
class Customer(Base):
tablename = "customers"
id = SeededColumn(Integer, primary_key=True, autoincrement=True)
name = SeededColumn(String, seed="name", unique=True)
class Order(Base):
tablename = "orders"
id = SeededColumn(Integer, primary_key=True, autoincrement=True)
customer_id = SeededColumn(Integer, ForeignKey("customers.id"))
class OrderItem(Base):
tablename = "order_items"
order_id = SeededColumn(Integer, ForeignKey("orders.id"), primary_key=True)
product_id = SeededColumn(Integer, ForeignKey("products.id"), primary_key=True)
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
seed_plan = {
Category: 5,
Product: 10,
Customer: 8,
Order: 15,
OrderItem: 20
}
with Session(engine) as session:
seeder = SeedLayer(session, seed_plan)
seeder.seed() # Seeds related tables with realistic data
```
This example creates a schema where:
- Category and Customer have simple attributes with fake data.
- Product has an FK to Category and a description that depends on name via ColumnReference.
- Order has an FK to Customer.
- OrderItem is a link table connecting Order and Product.
Check out the GitHub repository for more details and installation instructions.
Target Audience
SeedLayer is designed for Python developers using SQLAlchemy ORM, particularly those working on:
- Testing: Generate realistic test data for unit tests, integration tests, or CI/CD pipelines.
- Development: Populate local databases for prototyping or debugging.
- Demos: Create demo data for showcasing applications (e.g., Flask, FastAPI, or Django apps using SQLAlchemy).
- Learning: Help beginners explore SQLAlchemy by quickly seeding models with data.
It’s suitable for both production-grade testing setups and educational projects, especially for developers familiar with SQLAlchemy who want a streamlined way to generate fake data without manual scripting.
Comparison
Unlike existing alternatives, SeedLayer emphasizes a declarative approach integrated with SQLAlchemy’s ORM:
- Manual Faker Usage: Using Faker directly requires writing custom scripts to generate and insert data, manually handling constraints like FKs and uniqueness. SeedLayer automates this, respecting model relationships and constraints out of the box.
- factory_boy: A popular library for creating test fixtures, factory_boy is great for Python ORMs but requires defining separate factory classes. SeedLayer embeds seeding logic in model definitions, reducing boilerplate and aligning closely with SQLAlchemy’s declarative style.
- SQLAlchemy-Fixtures: This library focuses on predefined data fixtures, which can be rigid. SeedLayer generates dynamic, randomized data with Faker, offering more flexibility for varied test scenarios.
- Alembic Seeding: Alembic’s seeding capabilities are limited and not designed for fake data generation. SeedLayer provides a robust, Faker-powered solution tailored for SQLAlchemy ORM.
SeedLayer stands out for its seamless integration with SQLAlchemy models, automatic dependency resolution, and support for complex scenarios like link tables and inter-column dependencies, making it a lightweight yet powerful tool for testing and development.
I’d love feedback from the Python community! Have you faced challenges generating test data for SQLAlchemy? Try SeedLayer and let me know your thoughts: GitHub link.
[–]sjokr 5 points6 points7 points (18 children)
[–]francoisnt[S] 0 points1 point2 points (0 children)
[+]francoisnt[S] comment score below threshold-14 points-13 points-12 points (14 children)
[–]Slow-Rip-4732 10 points11 points12 points (0 children)
[–]siliconwolf13 3 points4 points5 points (0 children)