Mog0
Mog0

Reputation: 2129

Migration in PostgreSQL cast from string to uuid

One of my collegues added a field in one of our models as string instead of Guid so I corrected the type in a new migration but when I try to apply the migration I get the error:

"42804: column "last_modified_by_id" cannot be cast automatically to type uuid"

The database is PostgreSql.

The databases currently don't have any records in the table so I'm not worried about data loss in this table but the migration that created the field has been run on databases with data we need to keep in other tables.

Is there any way to do a custom cast or even to just drop all the data in the column rather than trying to cast it.

Upvotes: 2

Views: 3908

Answers (1)

Stefanov.sm
Stefanov.sm

Reputation: 13049

USING clause with an explicit casting expression will do the job. In your SQL console run this script:

ALTER TABLE _table 
 ALTER COLUMN last_modified_by_id 
 TYPE uuid 
 USING last_modified_by_id::uuid;

Upvotes: 4

Related Questions