Skip to content

Typed table creation required additional keyspace specification #52

@skedwards88

Description

@skedwards88

When creating a typed table and specifying the keyspace with client.getDatabase, the command fails with The database error: Keyspace 'default_keyspace' doesn't exist. You must again specify the keyspace with database.createTable. This only applies to typed tables, not untyped tables.

e.g. This fails:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateTableOptions;
import com.datastax.astra.client.tables.definition.columns.ColumnTypes;
import com.datastax.astra.client.tables.mapping.Column;
import com.datastax.astra.client.tables.mapping.EntityTable;
import com.datastax.astra.client.tables.mapping.PartitionBy;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import lombok.Data;

public class Example {
  @EntityTable("example_table")
  @Data
  public class Book {
    @PartitionBy(0)
    @Column(name = "title", type = ColumnTypes.TEXT)
    private String title;

    @Column(name = "number_of_pages", type = ColumnTypes.INT)
    private Integer number_of_pages;

    @Column(name = "rating", type = ColumnTypes.FLOAT)
    private Float rating;

    @Column(name = "genres", type = ColumnTypes.SET, valueType = ColumnTypes.TEXT)
    private Set<String> genres;

    @Column(
        name = "metadata",
        type = ColumnTypes.MAP,
        keyType = ColumnTypes.TEXT,
        valueType = ColumnTypes.TEXT)
    private Map<String, String> metadata;

    @Column(name = "is_checked_out", type = ColumnTypes.BOOLEAN)
    private Boolean is_checked_out;

    @Column(name = "due_date", type = ColumnTypes.DATE)
    private Date due_date;
  }

  public static void main(String[] args) {
    // Get an existing database
    DataAPIClient client = DataAPIClients.clientHCD(System.getenv("USERNAME"), System.getenv("PASSWORD"));
    Database database = client.getDatabase(System.getenv("API_ENDPOINT"), "quickstart_keyspace");

    Table<Book> table = database.createTable(Book.class);
  }
}

This works:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateTableOptions;
import com.datastax.astra.client.tables.definition.columns.ColumnTypes;
import com.datastax.astra.client.tables.mapping.Column;
import com.datastax.astra.client.tables.mapping.EntityTable;
import com.datastax.astra.client.tables.mapping.PartitionBy;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import lombok.Data;

public class Example {
  @EntityTable("example_table")
  @Data
  public class Book {
    @PartitionBy(0)
    @Column(name = "title", type = ColumnTypes.TEXT)
    private String title;

    @Column(name = "number_of_pages", type = ColumnTypes.INT)
    private Integer number_of_pages;

    @Column(name = "rating", type = ColumnTypes.FLOAT)
    private Float rating;

    @Column(name = "genres", type = ColumnTypes.SET, valueType = ColumnTypes.TEXT)
    private Set<String> genres;

    @Column(
        name = "metadata",
        type = ColumnTypes.MAP,
        keyType = ColumnTypes.TEXT,
        valueType = ColumnTypes.TEXT)
    private Map<String, String> metadata;

    @Column(name = "is_checked_out", type = ColumnTypes.BOOLEAN)
    private Boolean is_checked_out;

    @Column(name = "due_date", type = ColumnTypes.DATE)
    private Date due_date;
  }

  public static void main(String[] args) {
    // Get an existing database
    DataAPIClient client = DataAPIClients.clientHCD(System.getenv("USERNAME"), System.getenv("PASSWORD"));
    Database database = client.getDatabase(System.getenv("API_ENDPOINT"), "quickstart_keyspace");

    Table<Book> table = database.createTable(Book.class, new CreateTableOptions().keyspace("quickstart_keyspace"));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions