Insert a row (Go)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Inserts a single row into a table.
This method can insert a row in an existing CQL table, but the Data API does not support all CQL data types or modifiers. For more information, see Data type compatibility in tables (Go).
For general information about working with tables and rows, see About tables with the Data API (Go).
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Inserts the specified row and returns an *InsertOneResult struct that includes the primary keys of the inserted row.
If a row with the specified primary key already exists in the table, the row is overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Parameters
Use the InsertOne method, which belongs to the Table type.
Method signature
func (t *Table) InsertOne(
ctx context.Context,
row any,
opts ...options.TableInsertOneOption
) (*results.InsertOneResult, error)
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
A struct or map describing a row to insert. All primary key values are required. To reduce tombstones, you should not explicitly set a column to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata (Go). |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to insert a row into a table.
Insert a row
When you insert a row, you must specify a non-null value for each primary key column.
Non-primary key columns are optional.
To reduce tombstones, you should not explicitly set a column to null.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/datatypes"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
// Insert a row into the table
_, err := table.InsertOne(
ctx,
map[string]any{
"title": "Computed Wilderness",
"author": "Ryan Eau",
"number_of_pages": 432,
"due_date": datatypes.DateOnly{
Year: 2024,
Month: 12,
Day: 18},
"genres": []string{"History", "Biography"},
},
)
if err != nil {
log.Fatal(err)
}
}
Insert a row with vector embeddings
You can only insert vector embeddings into vector columns.
To create a table with a vector column, see Create a table (Go). To add a vector column to an existing table, see Alter a table (Go).
All embeddings in the column should use the same provider, model, and dimensions. Mismatched embeddings can cause inaccurate vector searches.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/datatypes"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
// Insert a row into the table
_, err := table.InsertOne(
ctx,
map[string]any{
"title": "Computed Wilderness",
"author": "Ryan Eau",
"summary_genres_vector": datatypes.NewVector(
[]float32{0.08, -0.62, 0.39},
),
},
)
if err != nil {
log.Fatal(err)
}
}
Client reference
For more information, see the client reference.