Ecto New Column Population

Hey guys,

I’ve created a migration for an Ecto / Postgres table to add a column for an embedded module. The problem is, I would like my existing records to all have this column populated with a default instance. Since embedded values have an id, I shouldn’t be doing this externally of my Phoenix app (and that’s going against the notion of migrations, anyway), so what is the preferred solution for this? Can a migration contain a rule to pre-populate existing records with this data?



I think it’s not super clear yet what you want to precisely do. Whenever you have a JSONB field (or similar in your db) you can still do default value on the database level, by specifying this value on creation of the column. Alternatively you can use either execute a raw SQL to populate the data, or use Ecto query and update_all.

I suspect, however, you might want to generate these IDs for each record, so it involves iterating over all records. If this is the case, and it might take a lot of time because you have millions of records, I would most likely defer to running it post-deployment as a script or command.

Again that’s sort of guessing here because I am not 100% clear what your problem really is.

Right, so that was my query :slight_smile:

If the column was a simple type, I could provide a default. As a complex type, I can write code to populate this manually in retrospect. I was simply wondering if there was a happy medium; to populate the column with a complex default specified in the migration.

Thanks Hubert :wink: