Alter a table (Python)
Alters a table by doing one of the following:
-
Adding one or more columns to a table
-
Dropping one or more columns from a table
You cannot change a column’s type. Instead, you must drop the column and add a new column.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (Python) and Create a vector index (Python).
|
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
Adds or drops columns.
Returns a Table instance that represents the table after the modification.
Although the Table instance that you used to perform the alteration will still work, it will not reflect the updated typing if you added or dropped columns.
To reflect the new typing, use the row_type parameter.
Parameters
Use the alter method, which belongs to the astrapy.Table class.
Method signature
alter(
operation: AlterTableOperation | dict[str, Any],
*,
row_type: type[Any] = DefaultRowType,
table_admin_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> Table[NEW_ROW]
| Name | Type | Summary |
|---|---|---|
|
|
The alter operation to perform. Can be one of the following:
|
|
|
Optional.
A formal specifier for the type checker.
If provided, |
|
|
Optional.
A timeout, in milliseconds, for the underlying HTTP request.
If not provided, the |
Examples
The following examples demonstrate how to alter a table.
Add columns to a table
When you add columns, the columns are defined in the same way as they are when you create a table.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (Python) and Create a vector index (Python).
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import (
AlterTableAddColumns,
ColumnType,
TableScalarColumnTypeDescriptor,
)
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Add columns
table.alter(
AlterTableAddColumns(
columns={
"is_summer_reading": TableScalarColumnTypeDescriptor(
column_type=ColumnType.BOOLEAN
),
"library_branch": TableScalarColumnTypeDescriptor(
column_type=ColumnType.TEXT
),
}
)
)
Add vector columns to a table
After you add a vector column, you should index the column if you want run vector searches on the column. For more information, Create a vector index (Python).
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import (
AlterTableAddColumns,
TableVectorColumnTypeDescriptor,
)
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Add a vector column
table.alter(
AlterTableAddColumns(
columns={
"example_vector": TableVectorColumnTypeDescriptor(
dimension=1024
),
}
)
)
Drop columns from a table
Dropping columns produces tombstones. Excessive tombstones can impact query performance.
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import AlterTableDropColumns
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Drop columns
table.alter(
AlterTableDropColumns(columns=["is_summer_reading", "library_branch"])
)
Client reference
For more information, see the client reference.