Seeding
Seeding is simply a way to quickly seed, or put data into your tables.

Creating Seeds

You can create a seed file and seed class which can be used for keeping seed information and running it later.
To create a seed run the command:
1
$ masonite-orm seed User
Copied!
This will create some boiler plate for your seeds that look like this:
1
from masoniteorm.seeds import Seeder
2
3
class UserTableSeeder(Seeder):
4
5
def run(self):
6
"""Run the database seeds."""
7
pass
Copied!
From here you can start building your seed.

Building Your Seed

A simple seed might be creating a specific user that you use during testing.
1
from masoniteorm.seeds import Seeder
2
from models import User
3
4
class UserTableSeeder(Seeder):
5
6
def run(self):
7
"""Run the database seeds."""
8
User.create({
9
"username": "Joe",
10
"email": "[email protected]",
11
"password": "secret"
12
})
Copied!

Running Seeds

You can easily run your seeds:
1
$ masonite-orm seed:run User
Copied!

Database Seeder

Factories

Factories are simple and easy ways to generate mass amounts of data quickly. You can put all your factories into a single file.

Creating A Factory Method

Factory methods are simple methods that take a single Faker instance.
1
# config/factories.py
2
3
def user_factory(self, faker):
4
return {
5
'name': faker.name(),
6
'email': faker.email(),
7
'password': 'secret'
8
}
Copied!
For methods available on the faker variable reference the Faker documentation.

Registering Factories

Once created you can register the method with the Factory class:
1
# config/factories.py
2
from masoniteorm import Factory
3
from models import User
4
5
def user_factory(self, faker):
6
return {
7
'name': faker.name(),
8
'email': faker.email(),
9
'password': 'secret'
10
}
11
12
Factory.register(User, user_factory)
Copied!

Naming Factories

If you need to you can also name your factories so you can use different factories for different use cases:
1
# config/factories.py
2
from masoniteorm import Factory
3
from models import User
4
5
def user_factory(self, faker):
6
return {
7
'name': faker.name(),
8
'email': faker.email(),
9
'password': 'secret'
10
}
11
12
def admin_user_factory(self, faker):
13
return {
14
'name': faker.name(),
15
'email': faker.email(),
16
'password': 'secret',
17
'is_admin': 1
18
}
19
20
Factory.register(User, user_factory)
21
Factory.register(User, admin_user_factory, name="admin_users")
Copied!

Calling Factories

To use the factories you can import the Factory class from where you built your factories. In our case it was the config/factories.py file:
1
from config.factories import Factory
2
from models import User
3
4
users = Factory(User, 50).create() #== <masoniteorm.collections.Collection object>
5
user = Factory(User).create() #== <models.User object>
Copied!
This will persist these users to the database. If you want to simply make the models or collection (and not persist them) then use the make method:
1
from config.factories import Factory
2
from models import User
3
4
users = Factory(User, 50).make() #== <masoniteorm.collections.Collection object>
5
user = Factory(User).make() #== <models.User object>
Copied!
Again this will NOT persist values to the database.

Calling Named Factories

By default, Masonite will use the factory you created without a name. If you named the factories you can call those specific factories easily:
1
from config.factories import Factory
2
from models import User
3
4
users = Factory(User, 50).create(name="admin_users") #== <masoniteorm.collections.Collection object>
Copied!

After Creating

You can also specify a second factory method that will run after a model is created. This would look like:
1
# config/factories.py
2
from masoniteorm import Factory
3
from models import User
4
5
def user_factory(self, faker):
6
return {
7
'name': faker.name(),
8
'email': faker.email(),
9
'password': 'secret'
10
}
11
12
def after_users(self, model, faker):
13
model.verified = True
14
15
Factory.register(User, user_factory)
16
Factory.after_creating(User, after_users)
Copied!
Now when you create a user it will be passed to this after_creating method:
1
user = factory(User).create()
2
user.verified #== True
Copied!

Modifying Factory Values

If you want to modify any values you previously set in the factory you created, you can pass a dictionary into the create or make method:
1
from config.factories import Factory
2
from models import User
3
4
users = Factory(User, 50).create({'email': '[email protected]'}) #== <masoniteorm.collections.Collection object>
Copied!
This is a great way to make constant values when testing that you can later assert to.
Last modified 9mo ago