Search code examples
sql-serverliquibase

Getting Liquibase SQL Server error 'Cannot find the object ... because it doesn't exist or you do not have permissions'


I have a demo SQL Server database running in a Docker container and I'm testing out Liquibase on it. I created a changelog file and put in two changesets that would remove two columns from a particular table.

This is my Liquibase changelog file:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:pro="http://www.liquibase.org/xml/ns/pro"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd
    http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd ">

    <changeSet id="1" author="mdailey">
        <dropColumn tableName="Sales.Invoices" columnName="RunPosition">
        </dropColumn>
    </changeSet>
    <changeSet id="2" author="mdailey">
        <dropColumn tableName="Sales.Invoices" columnName="DeliveryRun">
        </dropColumn>
    </changeSet>
</databaseChangeLog>

When I run liquibase update I keep getting the following error:

Unexpected error running Liquibase: Cannot find the object "Sales.Invoices" because it does not exist or you do not have permissions. [Failed SQL: (4902) ALTER TABLE [Sales.Invoices] DROP COLUMN RunPosition]

I'm using the sample database WideWorldImporters Microsoft provides. The schema is Sales so the table name is correct. I have a user and login named sqladmin and the login info is in the Liquibase properties file. Here's the properties file:

# Enter the path for your changelog file.
changeLogFile=changelog/db.changelog-root.xml

# Enter the Target database 'url' information #
liquibase.command.url=jdbc:sqlserver://localhost:1401;database=WideWorldImporters

# Enter the username for your Target database.
liquibase.command.username: sqladmin    

# Enter the password for your Target database.
liquibase.command.password: <StrongPassword>

I added sqladmin to the db_datawriter role so it has enough permissions. When I run a T-SQL statement in SQL Server as 'sqladmin' to drop the same column from the same table it executes successfully. I think the problem is with Liquibase but I can't figure what.


Solution

  • Thanks to @SMor suggestion I figured out the solution.

    In the changeset you don't add the schema name in front of the table, instead it needs to be added as a separate attribute.

    <changeSet id="1" author="mdailey">
            <dropColumn schemaName="Sales" tableName="Invoices" columnName="RunPosition">
            </dropColumn>
        </changeSet>