Seeders

Seeders populate the database with initial or test data. One per feature, executed in dependency order.

Table of contents

What they do

When you need users, sample data, or configuration records in the database, you write a seeder. The CLI runs all seeders in the right order so dependencies are satisfied (auth before profile, notes before notes_tags).


Creating a seeder

# splent_feature_auth/seeders.py
from splent_framework.seeders.BaseSeeder import BaseSeeder
from splent_io.splent_feature_auth.models import User

class AuthSeeder(BaseSeeder):
    def run(self):
        users = [
            User(email="user1@example.com", password="1234"),
            User(email="user2@example.com", password="1234"),
        ]
        self.seed(users)

Implement run() with your seeding logic. Call self.seed(data) to bulk insert.


Real example: profile seeder

Profile depends on auth — users must exist first. The seeder can safely assume users are already seeded because execution order follows UVL constraints:

# splent_feature_profile/seeders.py
from splent_framework.seeders.BaseSeeder import BaseSeeder
from splent_io.splent_feature_auth.models import User
from splent_io.splent_feature_profile.models import UserProfile

class ProfileSeeder(BaseSeeder):
    def run(self):
        users = User.query.all()
        names = [("John", "Doe"), ("Jane", "Doe")]

        profiles = []
        for user, (name, surname) in zip(users, names):
            if not user.profile:
                profiles.append(
                    UserProfile(user_id=user.id, name=name, surname=surname)
                )

        if profiles:
            self.seed(profiles)

No need to check if auth ran first — UVL guarantees it.


How seed() works

  1. Validates all objects are the same model type
  2. Bulk inserts via db.session.add_all()
  3. Commits the transaction
  4. Returns the list with database-assigned IDs

On IntegrityError (duplicate key, constraint violation), rolls back and raises SeederError.


Running seeders

splent db:seed                          # all seeders in UVL order
splent db:seed splent_feature_auth      # only auth
splent db:seed --reset -y               # wipe DB first, then seed all

Execution order

db:seed discovers all seeders.py files across declared features and runs them in UVL topological order — the same order features load at runtime. If profile requires auth in the UVL model, AuthSeeder always runs before ProfileSeeder.

No manual priority attribute. The UVL constraints handle it.


Convention

  • One seeders.py per feature
  • One seeder class per file
  • db:seed discovers it by finding the first class that extends BaseSeeder
  • Name it whatever you want (AuthSeeder, NotesSeed, etc.)

See also

  • db:seed — the CLI command that runs seeders
  • Repositories — the data layer seeders write to

Back to top

splent. Distributed by an LGPL license v3. Contact us: drorganvidez@us.es