The economic impact of the Ravencoin asset layer

Many volunteers have donated their time and energy to work on Ravencoin. Not only by writing code for first and second layer solutions, but also to test (new) features, mining main & test net…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Writing Queries with Schemas

Programming Ecto — Pragmatic Programmers (30 / 122)

👈 Creating Schemas | TOC | Inserting and Deleting with Schemas 👉

When we looked at queries in the last chapter, we deliberately chose to write them without schemas. This is still a good choice in some circumstances, but schemas provide some helpful shortcuts and we promised that we’d revisit them later. Now’s the time. In this section, we’ll start writing queries that work with schemas, and we’ll also consider when it’s best not to.

Let’s look at a query we ran in the last chapter:

In addition, we have to use the select option to specify what columns we want returned to us. This is not so bad since we’re only fetching one value, but consider what this would look like with the tracks table:

Specifying all those fields every time could get tedious quickly.

Schemas can help us with both of these issues. Using our new %Track{} schema, we can rewrite the query like this:

Notice that we’ve replaced the string “tracks” with the name of the schema module we created earlier. This tells Ecto that our query is referencing a schema, rather than the name of a database table. Adding the alias in the first line lets us use the module name without its full namespace — most Ecto developers follow this convention.

Add a comment

Related posts:

Wrapping Up

Software delivers its value in production. The development project, testing, integration, and planning…everything before production is prelude. This book deals with life in production, from the…

My Super Power at Work

My last job assignment came with its own skill set. A cog in a huge business organization, I was the executive office manager to the top senior VP. Every company has its own pecking order. Basically…

Entrepreneurship Project

The strategy behind these tasks which we designed under full of hard work and deep discussions where we faced bit conflicts as well because of some opposition to perceptions of fellows but we…