![]() Timestamps() adds inserted_at and updated_at columns. id is set as the primary key, and name completes the list of ones we created. # priv/repo/migrations/20210414205427_create_teachers.exs defmodule do use Ecto.MigrationĬreate table( :teachers, primary_key: false) doīy default, we get 4 colums created. This generates a migration that creates out teachers table and a schema file. We need a teachers table and schema so lets fire up the generator $ mix Teacher teachers name:string However, uuids are harder to blindly guess and will be useful if later you decide to spread your data out to multiple systems. They are faster and arguably easier to index. You are more than welcome to use the default autoincrement integers instead. Setting the -binary-id flag ensures we'll be generating uuid primary keys This is a column thatserves as a unique key for a particular record in a table.īy default, phoenix sets up autoincrementing integers as the primary keys. ![]() mix phx.new -binary-id -database postgres grade_trackerĮvery record in yoru database needs a primary key. Now that we have a birds eye map of the schema we want to build and model, we can start to hash out the details in code! Ecto Schema Fundamentalsįirst, install the phoenix generator and run the following to create your project. An Enrollment joins a Student and Class to establish a many to many relationship.With that we can add a few more relationships and associated constrants ![]() It makes sense to think about it in terms of the Enrollment of the students. Thinking about grades, I would model it as an operation over a group of Assignment. We still havent gotten to grades! But where to store it? a Teacher has many Student through a Class.a Student has many Teacher through a Class.Given the following the information, we can also infer some secondary effect Right here we have a few relationships mapped out. A Class has many Student and only one Teacher: many to many.This could very well be the heart of a system that helps etachers quickly grade their students and send out report cards. To make this all a bit more relatable, lets plan a schema for a school grade tracker. A has many B through C (if B also has_many A through C, we might call this a many to many relationship)Īny nontrivial database schema is goign to have all of these relationships modeled in one way or another.The secret to thinking in sql is right here in the word "relational." Almost all relations you will model fit one of three catagroies. I'll spare you the theoratical gibber jabber about industry jargon like "third normal form" and "denormalization".įrankly, a lot of books on sql modeling get high on the theoretical horse to cover every single possible edge case imaginable.Īs somone who learned sql on the job, I've distilled it down to a a few rules of thumb with the occasional exception. Of course, this means you need to know how to think in sql and the relational model. SQL Land however, does not deal with objects.Įcto takes SQL as it is an gives you very powerful tools for modeling yoru application's database layer without building abstractions that ultimatly hinder you later on with slow queries and magic behavior that gets in your way. ![]() ORMs like active record or django's python objects try to hide sql behind their language specifc interfaces. If you're new to Elixir, you're probably finding ecto to be a bit of a culture shock.Įcto is an immensly powerful library for working with sql but Unlike other ORMs you may have used, Ecto embraces what makes sql great. You've heard of elixir and are just getting started with your first taste of the kool-aid. Note: original can be found here Creating your first records Contents ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |