Changing Field Types in Ecto Embedded Schemas: A Migration Guide

4
clicks
Changing Field Types in Ecto Embedded Schemas: A Migration Guide

Source: dreamingecho.es

Type: Post

The content discusses the challenges faced when changing the type of a field in an Ecto embedded schema, specifically converting a field from a string to a list of strings while preserving existing data stored in PostgreSQL as JSONB. It outlines the need for a comprehensive migration since there is no direct alter command for embedded schemas. The article details the process of writing a migration that includes SQL functions for transforming data and ensuring data integrity during the transformation. Additionally, it emphasizes the necessity of updating Ecto queries to accommodate the new field type. The article concludes by highlighting the importance of safely handling JSONB fields in PostgreSQL during migrations.

© HashMerge 2025