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 field contact_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)?

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.)

1 Like

Thanks for the explanation.
I rechecked everything and found that I had missed the step where it says

mix ecto.migrate

Now it works fine. Thanks!