Alter table add if not exists Cassandra

Viewed 770

I have an update script I created and run before.

ALTER TABLE "facilities" ADD "tariff_id" text
GO 

I want to run this query again without deleting it from the script. If exists did not work with alter. How can I do it? I have a this exception:

Cassandra.InvalidQueryException: 'Invalid column name tariff_id because it conflicts with an existing column'

2 Answers

In your script, you can verify if the column exists querying the system_schema in cassandra. Is very simple in your case will be like this:

select * from system_schema.columns where keyspace_name = 'YOUR_KEYSPACE' and table_name = 'facilities' and column_name = 'tariff_id';

If return no lines mean that your column doesn't exists.

Reference: https://docs.datastax.com/en/dse/5.1/cql/cql/cql_using/useQuerySystemTable.html

I could not find an answer to this problem. The best solution was to ignore that exception code. My solution:

try
{
    DB.Instance.Execute(script);
    script = "";
}
catch (Exception e)
{
    //It helps to pass the lines that already exist. Alter table add etc.
    if ( HResult == (-2146233088))
        script = "";
    else 
        throw e;
}
Related