I am using MySQL, using Liquibase (Liquibase Version: 4.23.1), I am generating a table in the changeset and in another changeset, I am using precondition to check if the primary key exists and then only insert the record.
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="adam" id="1693111591793-1">
<createTable catalogName="studentdb" tableName="student">
<column autoIncrement="true" name="student_id" type="BIGINT">
<constraints nullable="false" primaryKeyName="pk_student_id" primaryKey="true" />
</column>
<column name="student_name" type="VARCHAR(20)" />
</createTable>
</changeSet>
<changeSet author="adam" id="1693111591793-2">
<preConditions>
<primaryKeyExists tableName="student" primaryKeyName="pk_student_id" />
</preConditions>
<insert catalogName="studentdb" tableName="student">
<column name="student_id" valueNumeric="1" />
<column name="student_name" value="Mac" />
</insert>
</changeSet>
</databaseChangeLog>
the command is liquibase update
The id column has constraints
<constraints nullable="false" primaryKeyName="pk_student_id" primaryKey="true" />
but still the precondition
<preConditions>
<primaryKeyExists tableName="student" primaryKeyName="pk_student_id" />
</preConditions>
throwing error as below
Migration failed for changeset changelog.mysql.xml::1693111591793-2::adam:
Reason:
changelog.mysql.xml : Primary Key does not exist on the student
- Caused by: Preconditions Failed
what am i missing?