I'm trying to create two tables with relation one to many using liquibase and postgresql in java springboot application. There is only one entry in changelog.
When starting application I receive this error:
liquibase.exception.DatabaseException: ERROR: relation "public.main" does not exist [Failed SQL: (0) CREATE TABLE public.relation (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, main_id BIGINT NOT NULL, CONSTRAINT "PK_relation_id" PRIMARY KEY (id), CONSTRAINT "FK_relation_main" FOREIGN KEY (main_id) REFERENCES public.main(id), UNIQUE (id))]
Tables definition:
{
"databaseChangeLog": [
{
"changeSet": {
"id": "initial-structure-create",
"author": "tcyborowski",
"changes": [
{
"createTable": {
"tableName": "main",
"columns": [
{
"column": {
"name": "id",
"type": "bigint",
"autoIncrement": true,
"constraints": {
"primaryKey": true,
"primaryKeyName": "PK_main_id",
"nullable": false,
"unique": true
}
}
},
{
"column": {
"name": "name",
"type": "varchar(255)",
"constraints": {
"nullable": false,
"unique": false
}
}
}
]
},
"createTable": {
"tableName": "relation",
"columns": [
{
"column": {
"name": "id",
"type": "bigint",
"autoIncrement": true,
"constraints": {
"primaryKey": true,
"primaryKeyName": "PK_relation_id",
"nullable": false,
"unique": true
}
}
},
{
"column": {
"name": "main_id",
"type": "bigint",
"constraints": {
"nullable": false,
"unique": false,
"referencedTableSchemaName": "public",
"referencedTableName": "main",
"referencedColumnNames": "id",
"foreignKeyName": "FK_relation_main"
}
}
}
]
}
}
]
}
}
]
}
liquidbase version 4.3.3 - the same problem is with h2 in-memory database
If I split this json tables declaration into two json change files, run first table creation and after that second table creation tables are created without problem with correct relation.
If I try to run this this on clean database I get the same error as above:
{
"databaseChangeLog": [
{
"include": {
"file": "db/changelog/partials/initial-structure.json"
},
"include": {
"file": "db/changelog/partials/initial-structure2.json"
}
}
]
}
What could be causing problem with running this in one go?
Ok, I found my mistake after changing logging to debug.
In both my json files above there is an error which caused only last action to be executed.
Corrected json files below:
{
"databaseChangeLog": [
{
"include": {
"file": "db/changelog/partials/initial-structure.json"
}
},
{
"include": {
"file": "db/changelog/partials/initial-structure2.json"
}
}
]
}
{
"databaseChangeLog": [
{
"changeSet": {
"id": "initial-structure-create",
"author": "tcyborowski",
"changes": [
{
"createTable": {
"tableName": "main",
"columns": [
{
"column": {
"name": "id",
"type": "bigint",
"autoIncrement": true,
"constraints": {
"primaryKey": true,
"primaryKeyName": "PK_main_id",
"nullable": false,
"unique": true
}
}
},
{
"column": {
"name": "name",
"type": "varchar(255)",
"constraints": {
"nullable": false,
"unique": false
}
}
}
]
}
},
{
"createTable": {
"tableName": "relation",
"columns": [
{
"column": {
"name": "id",
"type": "bigint",
"autoIncrement": true,
"constraints": {
"primaryKey": true,
"primaryKeyName": "PK_relation_id",
"nullable": false,
"unique": true
}
}
},
{
"column": {
"name": "main_id",
"type": "bigint",
"constraints": {
"nullable": false,
"unique": false,
"referencedTableSchemaName": "public",
"referencedTableName": "main",
"referencedColumnNames": "id",
"foreignKeyName": "FK_relation_main"
}
}
}
]
}
}
]
}
}
]
}