- Phoenix Web Development
- Brandon Richey
- 544字
- 2021-08-27 18:28:28
Creating our Poll schema
A schema in Ecto needs one use statement and one import statement by default. In addition, we want to namespace the modules that we define for our new schema with the name of the app (but not the web app!) and the name of the context. We created votes as the subdirectory under the app "Vocial", so we'll create our module as Vocial.Votes.Poll. Next, we need to tell Ecto that this file will be using all of the Ecto functions and helpers that are available to us.
The schema will also be in charge of telling Ecto how to build changesets to modify data in our database, so we'll need to include that functionality via an import statement. We’ll also want to make our lives easier by including an alias for our full module definition so that we can just say %Poll{} instead of %Vocial.Votes.Poll{}. So we’ll start off building our file like this:
defmodule Vocial.Votes.Poll do
use Ecto.Schema
import Ecto.Changeset
alias Vocial.Votes.Poll
end
Next, we need to tell Ecto how to read rows from our database and our polls table as Elixir data structures, so we need to define a Schema. Let's add the following block to our file:
schema "polls" do
field :title, :string
timestamps()
end
Now we've told Ecto how to define our data and how to pull information out of the database. The final piece that's required for any schema file in Ecto is defining the changeset. Let's add a changeset function that will tell Ecto how to make modifications and insert or update those rows in our database. Typically, a changeset takes in the Struct representing the data structure you want to modify and the attributes you want to give to that data structure. You can think of it this way: a changeset always performs modifications, so if you're creating a new row in the database, you'd start with a blank object and roll your changes on top of that. If you're modifying an existing row, you'd take the Ecto representation of that existing row and perform modifications on top of that!
Next, your changeset has to take that initial representation and determine what columns are allowed to be modified (this is because we may have something like virtual fields, which are columns that don't actually exist in the database). This helps prevent against issues like mass assignment, where maybe you don’t want the application to be able to set certain columns as modifiable.
Finally, your changeset should validate whichever columns are required for this to be valid. This will make sure that in the final representation that goes into the database, all of the columns marked as required have values in them. This means that update statements do not have to include updates for columns that already have data in them, so you don't have to worry about validations being performed on columns that already have data in them! All of this talk is great and all, but let's take a look at what the code for that actually should be:
def changeset(%Poll{}=poll, attrs) do
poll
|> cast(attrs, [:title])
|> validate_required([:title])
end
We now have a fully-built and fully-functional representation of our Poll object in Ecto! Hooray!
- MERN Quick Start Guide
- 智能網(wǎng)聯(lián)汽車V2X與智能網(wǎng)聯(lián)設(shè)施I2X
- 物聯(lián)網(wǎng)網(wǎng)絡(luò)安全及應(yīng)用
- Spring Boot 2.0 Projects
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計(jì)
- 社交電商運(yùn)營(yíng)策略、技巧與實(shí)操
- 通信簡(jiǎn)史:從信鴿到6G+
- Mastering Dart
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- The Kubernetes Workshop
- TD-LTE無(wú)線網(wǎng)絡(luò)規(guī)劃與設(shè)計(jì)
- Scala Design Patterns.
- 互聯(lián)網(wǎng)+思維與創(chuàng)新:通往未來(lái)的+號(hào)
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- 端到端QoS網(wǎng)絡(luò)設(shè)計(jì)