# C# driver keyspace already exists exception

**URL:** <https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624>\
**Category:** General\
**Created:** [February 7, 2020, 6:53pm UTC](https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624 "2020-02-07T18:53:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://forum.yugabyte.com/u/victor)\
**Post date:** [February 7, 2020, 6:53pm UTC](https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624/1 "2020-02-07T18:53:35Z")

</div>

Hi. I moved from cassandra to yugabyte. This code was worked with latest cassandra c# driver, but generates exception with latest yugabyte c# driver:

```
 _cluster = Cluster.Builder()
                        .AddContactPoints(seeds)
                        .WithQueryOptions(new QueryOptions().SetConsistencyLevel(ConsistencyLevel.One))
                        .WithDefaultKeyspace("mykeyspace").Build(); 
Session = _cluster.ConnectAndCreateDefaultKeyspaceIfNotExists(ReplicationStrategies
                        .CreateSimpleStrategyReplicationProperty(replicationFactor));
------------
Cassandra.InvalidQueryException: 'Keyspace Already Exists. Keyspace 'mykeyspace' already exists
CREATE KEYSPACE "mykeyspace" 
^^^^^^
  WITH replication = {'class' : 'SimpleStrategy', 'replication_factor' : '1'} 
   AND durable_writes = true
 (ql error -307)'

```

---

<div class="post-metadata">

**Author:** ![karthik](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/karthik/32/13_2.png) [@karthik](https://forum.yugabyte.com/u/karthik)\
**Post date:** [February 9, 2020, 6:41am UTC](https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624/2 "2020-02-09T06:41:46Z")

</div>

Hi @victor,

Adding a few folks to see if they have any idea. cc @Oleg @mihnea @kannan

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [February 10, 2020, 9:23am UTC](https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624/3 "2020-02-10T09:23:44Z")

</div>

[ConnectAndCreateDefaultKeyspaceIfNotExists](https://github.com/datastax/csharp-driver/blob/f38d0ddb6f21072219030fc11986570a2ce92b9f/src/Cassandra/Cluster.cs#L376) function calls [CreateKeyspaceIfNotExists](https://github.com/datastax/csharp-driver/blob/f38d0ddb6f21072219030fc11986570a2ce92b9f/src/Cassandra/Session.cs#L159) which tries to create a keyspace and capture the exception. [I think we fail on capturing the exception](https://github.com/datastax/csharp-driver/blob/d14c6b4f90e6ccb4087c32a2444d6b7ce1487793/src/Cassandra/Exceptions/AlreadyExistsException.cs#L23) ?

Yugabyte response:

```auto
cqlsh> CREATE KEYSPACE e_xists WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
cqlsh> CREATE KEYSPACE e_xists WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Keyspace Already Exists. Keyspace 'e_xists' already exists
CREATE KEYSPACE e_xists WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
^^^^^^
 (ql error -307)"

```

Cassandra response:

```auto
cqlsh> CREATE KEYSPACE e_xists WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
cqlsh> CREATE KEYSPACE e_xists WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
AlreadyExists: Keyspace 'e_xists' already exists

```

@victor  
For the meantime, can you execute connect to the cluster without creating a default keyspace ?  
And issue a `create keyspace "mykeyspace" if not exists ...` and not

---

<div class="post-metadata">

**Author:** ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://forum.yugabyte.com/u/victor)\
**Post date:** [February 10, 2020, 4:20pm UTC](https://forum.yugabyte.com/t/c-driver-keyspace-already-exists-exception/624/4 "2020-02-10T16:20:30Z")

</div>

1. “can you execute connect to the cluster without creating a default keyspace” - exception on connect because I defined default keyspace in cluster build.
2. comment cluster build default keyspace definition:  
\_cluster = Cluster.Builder()  
.AddContactPoints(seeds)  
.WithQueryOptions(new QueryOptions().SetConsistencyLevel(ConsistencyLevel.LocalQuorum))  
/\*.WithDefaultKeyspace(“e\_xists”)\*\*/.Build();
3. without “IF NOT EXISTS” - first connect: ok, second connect: exception “Keyspace Already Exists. Keyspace ‘e\_xists’ already exists”
4. with “IF NOT EXISTS” - first connect and next are ok.
