更改表添加(如果不存在)Cassandra

时间:2020-08-05 13:17:35

标签: c# cassandra

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

ALTER TABLE "facilities" ADD "tariff_id" text
GO 

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

Cassandra.InvalidQueryException:'无效的列名riff_id 因为它与现有的列相冲突'

2 个答案:

答案 0 :(得分:3)

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

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

如果不返回任何行,则表明您的列不存在。

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

答案 1 :(得分:0)

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

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;
}
相关问题