fireproofsocks

fireproofsocks

Documenting Ecto schemas

I’m working to document a large Elixir project and I’ve noticed that the Ecto schemas do not provide detailed types for their structs.

The best I’ve come up with is to include

@type t :: %__MODULE__{}

inside my Ecto schema modules. This at least causes the shape of the struct to show up in the docs, but it’s not what I would consider “polished”. In the generated ExDoc pages, the type includes the __meta__ attribute (which is confusing to any onlooker unfamiliar with Elixir) and all the values are represented with the term() catch-all:

@type t() :: %MyEctoSchema{
  __meta__: term(),
  foo: term(),
  bar: term(),
  # ...

The source-code says “Generating typespecs for schemas is out of the scope of Ecto.Schema" and that "t/0 has to be defined manually”… but when I tried manually adding my struct fields (in the way I would if this were a normal non-Ecto struct), my definitions all seem to be ignored regardless – it seems that no matter what I define, the output is the same default (with the __meta__ field and every field type as term()).

I did some noodling when I put together the inspecto package, but I don’t think it helps here since I don’t think you can dynamically assemble a type definition (or maybe you can in a macro?).

I think I’m probably missing something so I thought I’d do a sanity check here in the forum. Thanks for any insights!

Marked As Solved

LostKobrakai

LostKobrakai

I just tried it out in a project of mine and the docs update just fine for explicitly listed keys.

Also Liked

LostKobrakai

LostKobrakai

A ecto schema has a __meta__ field if you want it or not. It’s used to store metadata (see Ecto.get_meta). Doing %MyEctoSchema{} will make sure all keys of the schema are part of the typespec no matter if you explicitly type them or not.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement