【问题标题】:Alter table add if not exists Cassandra如果不存在则更改表添加 Cassandra
【发布时间】:2020-11-25 16:21:39
【问题描述】:

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

ALTER TABLE "facilities" ADD "tariff_id" text
GO 

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

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

【问题讨论】:

  • 由于数据库不支持该功能,只能处理错误。如果 cassandra 不支持 try/catch 之类的东西,那么它就是这样。
  • 感谢 insane_developer 的热情回复。

标签: c# cassandra


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-03
      • 1970-01-01
      • 2013-05-26
      • 2014-11-01
      • 1970-01-01
      • 2012-06-16
      • 2014-07-04
      • 2014-08-25
      • 2014-04-06
      相关资源
      最近更新 更多