Commands
Masonite ORM comes with several terminal commands you can run to speed up your development. Here are a list of commands and their descriptions
Below are examples to use Masonite ORM standalone but if you are using Masonite ORM with Masonite then you can replace the calls to masonite-orm
with your applications python craft
file.
For example you would replace the call for:
with
Migrations
Migration commands are used to create migration files, roll them back and refresh your database
Creating
You can create migration files easily. Migration files will create a class with an up
method and a down
method. You should perform your schema logic in the up
method and then reverse what you did in the down
method.
Migrating
Migrating will run each unmigrated migration's up
method. Each group of migrations that are ran will create a batch number and store information in the migrations
table. The batch number will allow groups of migrations to be rolled back if needed.
Rollback
You can rollback your migration files as well. This would run your migration files that are already migrated but in reverse order. This will run the down
method on each migration file in reverse order to "undo" the migration changes that were previously ran.
When a group of migrations are migrated, that group will create a batch number. The rollback command will only rollback the last batch, or the migrations that were ran in the last group of migrations that you ran.
Resetting
While the rollback method will rollback the previous batch of migrations, the reset command will rollback all migrations.
Refreshing
Refreshing migrations is a simple way to redo all of your migrations. First it will rollback all migrations and then automatically migrate all the migrations again.
Getting Migration Status
Sometimes its good to know the status of the migrations so you can know if you have any migrations that need to be ran:
Creating Models
You can create a new model class quickly. There are also several options you can pass to this command to quickly create migrations and seeds quickly.
Model Docstrings
One of the downsides of Masonite ORM compared to other models is you don't know what columns and data types you have on your models / tables.
For example, on other ORMs, columns are class attributes on your models so you can always reference your models to know what your tables look like.
To solve this with Masonite ORM you can use the model:docstring
command. This command will output an example docstring of all your tables columns and their data types so you can put it on your model for reference. When you make schema changes you can rerun this command to get the updated schema.
Another downside is not being able to see IDE type hints on your models. This is also solved using the --type-hints
option you can find below.
Make Observers
You can easily build observer files:
Seeding
Seeding is a great way to get test data into your database.
Making a Seed File
To make a seed file is simple:
Running Seeds
To run the seeds is simple:
Last updated