This is what we mean when we say that schemas are used to map external data into Elixir structs. Notice that the schema has the same fields as our migration file. The generated schema files can be found in the lib/catalog directory if we look at the schema generated in the store.ex file: defmodule Catalog.Store do The -no-migrate flag signals not to create a migration file, because we already created them. The other arguments are the fields we want to create. The first argument to the is the schema module, followed by its plural name, which should be the same as the table name. Mix Product products name:string description:string price:float -no-migration Mix Store stores name:string -no-migration To create our schemas, replicate the commands below: mix User users name:string -no-migration This means that these schemas allow us to have Elixir representations of external data (e.g., database tables). Schemas are used to map external data into Elixir structs. Next, add the code below to the create_product migration file: defmodule doĪfter the migrations are created, run the mix ecto.migrate command to make any changes to the database. Then, we’ll add the following code to the create_user migration file: defmodule do In the create_store migrate file, we are simply creating a stores table, then we add the name field and timestamps: defmodule doįollowing the steps above, we create two more migrations: create_user and create_product. Migration names usually take the format of _.exs. The newly created migration can be found in the priv/repo/migrations directory. To create our store migration, we use the following command: mix create_store The command has the following structure: mix These changes are usually made incrementally so they can be reversed at any point in time.Įcto provides a simple command for creating migrations. Database migrationsĭatabase migrations are a way of telling the database what changes we need to make. Make sure to have a database instance running, which you can check out in this tutorial for how to run PostgreSQL on Docker Once the application is created, cd catalog, edit the configuration at config/dev.exs to match your database credentials. We can do so by running the following command: mix phx.new catalog Our first step is to create a Phoenix application.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |