#2015·atlas

Atlas migrate diff picking random order for GORM models

Author: RoccoriuCreated Aug 23, 2023Updated Sep 29, 2026

I've been trying to get GROM working with atlas and it doesn't really work as expected. Out of 10 times trying to generate the migrations, it succeeds once and then throws errors about non-existent related tables on the other 9 attempts. I've defined the GORM models in the correct order, so I'm not quite sure why it's using a random order. While the screenshot shows it working on occasion, it's mostly down to luck, since you have to hope for the correct order to be used.

image

Additionally, I've also tried using the application mode instead of the standalone mode and that doesn't work either. Is this behaviour expected? Also shouldn't it use the previous migrations to create the base line schema to then generate the differences?

This is my atlas.hcl file, created according to the documentation, perhaps I'm doing something wrong?

data "external_schema" "gorm" {
  program = [
    "go",
    "run",
    "-mod=mod",
    "ariga.io/atlas-provider-gorm",
    "load",
    "--path", "./db",
    "--dialect", "postgres", // | postgres | sqlite
  ]
}

env "gorm" {
  src = data.external_schema.gorm.url
  // actual database to apply the migrations to
  url = "postgresql://user:[email protected]/aso" 

  // temporary database, usually used for
  dev = "postgresql://user:[email protected]/aso_dev" 
  migration {
    dir = "file://db/migrations"
  }
  format {
    migrate {
      diff = "{{ sql . \"  \" }}"
    }
  }
}