xataio / pgroll

PostgreSQL zero-downtime migrations made easy
https://www.xata.io
Apache License 2.0
2.97k stars 54 forks source link

References to tables from the same migration don't work #347

Open ilyakooo0 opened 4 months ago

ilyakooo0 commented 4 months ago
{
  "name": "1_initial_schema",
  "operations": [
    {
      "create_table": {
        "columns": [
          {
            "name": "id",
            "nullable": false,
            "pk": true,
            "type": "uuid",
            "unique": true
          }
        ],
        "name": "lang"
      }
    },
    {
      "create_table": {
        "columns": [
          {
            "name": "id",
            "nullable": false,
            "pk": true,
            "type": "uuid",
            "unique": true
          },
          {
            "name": "lang",
            "nullable": false,
            "pk": true,
            "references": {
              "column": "id",
              "name": "translation_lang_lang_id",
              "on_delete": "RESTRICT",
              "table": "lang"
            },
            "type": "uuid",
            "unique": false
          }
        ],
        "name": "translation"
      }
    }
  ]
}

When I try to reference a table added within the same migration I get the following error:

Error: migration is invalid: column reference to column "lang" in table "translation" is invalid: table "lang" does not exist

This is really misleading and results from the way temporary tables are created during a migration.

I think pgroll should ccount for the temporary table names and create the references using temporary table names.

ilyakooo0 commented 4 months ago

This issue also means that it is not always possible to encode all desired changes in a single migration.

andrew-farries commented 4 months ago

Hi @ilyakooo0 👋 , thanks for opening the issue.

The inability to reference objects created in earlier operations in the same migration is tracked in https://github.com/xataio/pgroll/issues/239 as part of the v1 milestone.

It is a significant limitation and one that will need to be removed before a v1.