Field in schema conflicting with earlier migration

hey all! long story short:

  • created new migration to add new column
  • add new column as field to schema file
  • works fine and migrates
  • upon resetting db and running all migrations, conflict from earlier migration which queries schema Repo.all(Table)
  • seems like the problem is that the schema claims there is a column and the earlier migration doesn’t agree the column exists.

details:

I’ve created a migration to add a new column to an existing table. I’ve added said column as a field in the schema file. Running this migration works perfectly.

when I reset the DB and run all of the migrations, i run into an error ** (Postgrex.Error) ERROR 42703 (undefined_column) column v0.newly_added_column_name does not exist .

I see an earlier timestamped migration calls to the table with case Repo.all(Table) do , and upon reading more Ecto docs i believe this line of code is referring to the Schema file with a field that hasnt been created yet since the migration which creates this column comes later. Is there a way to handle this outside of touching migration files? my initial idea is to recreate this migration w/ the line case Repo.all(Table) in order to let the migration which adds the new column happen first. I’d prefer not to touch previous migrations, but can if need be!

Try to comment the part where You query inside the migration. It should be easy to rollback and migrate… but You have added blocking code.

Without migrations, it’s hard to tell.

2 Likes

just clarifying that the code is blocking is enough for me to carry on my next approach :slight_smile: thanks @kokolegorille . from here on out it’ll be schema-less queries in migrations!

FWIW, you can avoid problems like this by defining a local version of the Table schema inside the migration file - that way the code in the migration doesn’t get surprised by unexpected fields.

4 Likes

Did not know about this, thanks :slight_smile: