改变 table 如果不存在则添加 Cassandra

Alter table add if not exists Cassandra

我有一个我创建的更新脚本,之前 运行。

ALTER TABLE "facilities" ADD "tariff_id" text
GO 

我想再次 运行 此查询而不将其从脚本中删除。如果存在则不适用于更改。我该怎么做? 我有一个例外:

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

在您的脚本中,您可以通过查询 cassandra 中的 system_schema 来验证该列是否存在。很简单在你的情况下会是这样的:

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

如果return没有行表示您的列不存在。

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

我找不到这个问题的答案。最好的解决方案是忽略该异常代码。我的解决方案:

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;
}