繁体   English   中英

如果不存在则更改表添加 Cassandra

[英]Alter table add if not exists Cassandra

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

ALTER TABLE "facilities" ADD "tariff_id" text
GO 

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

Cassandra.InvalidQueryException:'无效的列名关税_id,因为它与现有列冲突'

在您的脚本中,您可以通过查询 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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM