
hoeppner
From Ruby to Elixir — warning about invalid association when doing mix ecto.create (page 97)
Hi @sb8244,
I get a warning when I try to do the migrations on page 97 with mix ecto.create
:
warning: invalid association
sms_messages
in schema PhoneApp.Conversations.Schema.Contact: associated schema PhoneApp.Conversations.Schema.SmsMessage does not have fieldcontact_id
lib/phone_app/conversations/schema/contact.ex:1
I wanted to ask if this is because of the following:
The “SmsMessage” module has a clause that says schema "sms_messages" do
. That is, the name of the module has the singular form, but the name of the schema is plural.
In case of the “Contact” module, the situation is reversed. It has an equivalent clause that says schema "contacts" do
.
My confusion starts here:
The “SmsMessage” module refers to the contact s via belongs_to :contact
(note the singular form in the token), while the “Contact” module refers to the SMS messages via has_many :sms_messages
(note the plural).
Do the tokens’ (:contact
, :sms_messages
) forms (singular/plural) need to agree with the words in the schema
clauses (i.e., schema "contacts" do
and schema "sms_messages" do
)?
Most Liked

sb8244
Which page are on on when you get this? One section of the chapter calls out that you’ll get a warning because we haven’t set up the other side of the association yet. In that case, the warning goes away a few pages later.
The plurality in the book is correct. Has many is plural, belongs to is singular. Schema modules are singular but the table name is plural (table name actually doesn’t matter.)
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /centos
- /ash
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /lua
- /nodejs
- /c
- /julia
- /slackware
- /neovim