Alter a user-defined type (UDT) (C#)
Adds and renames fields in a user-defined type (UDT).
This command can’t rename the user-defined type itself or drop fields from the user-defined type. Instead, you must drop the user-defined type and then re-create the user-defined type with the desired name and fields.
|
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 renames fields in a user-defined type.
Does not return anything.
Parameters
Use the AlterTypeAsync method, which belongs to the Database class.
You can also use AlterType, which is the synchronous version of the method.
Method signature
public Task AlterTypeAsync(
string typeName,
IAlterTypeOperation operation,
AlterTypeOptions options = null
);
public Task AlterTypeAsync<T>(
IAlterTypeOperation operation,
AlterTypeOptions options = null
) where T : new();
| Name | Type | Summary |
|---|---|---|
|
|
The name of the user-defined type to alter. If you use a custom user-defined type class and do not specify this parameter, the client attempts to extract it from first from the |
|
|
The alter operation to perform. Can be one of the following:
See the examples for usage. |
|
Optional. General API options for this operation, including the timeout. For more information and examples, see Customize API interaction. |
Examples
The following examples demonstrate how to alter a user-defined type.
Rename fields
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Rename fields in a user-defined type
await database.AlterTypeAsync(
"member",
new AlterTypeRenameFields(
new() { ["name"] = "first_name", ["is_active"] = "is_member" }
)
);
}
}
Add fields
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Tables;
using DataStax.AstraDB.DataApi.Utils;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Add fields to a user-defined type
await database.AlterTypeAsync(
"member",
new AlterTypeAddFields(
new()
{
["email"] = DataAPIType.Text(),
["credits"] = DataAPIType.Int(),
}
)
);
}
}
Rename and add fields
You cannot use the client to rename and add fields in the same call. Make separate calls, or use HTTP directly.
Client reference
For more information, see the client reference.