简体   繁体   English

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

[英]Alter table add if not exists Cassandra

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.如果存在不适用于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' Cassandra.InvalidQueryException:'无效的列名关税_id,因为它与现有列冲突'

In your script, you can verify if the column exists querying the system_schema in cassandra.在您的脚本中,您可以通过查询 cassandra 中的 system_schema 来验证该列是否存在。 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参考: 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;
}

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

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