0

I am trying to create a dynamodb table using boto3. But I am getting the following error:

"botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: Invalid KeySchema: The first KeySchemaElement is not a HASH key type"

More info: I don't have any global table existing in my account.

Code I tried with:

import boto3

client = boto3.client('dynamodb')

response = client.create_table(
    AttributeDefinitions=[
        {
            'AttributeName': 'student_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'student_name',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'course_id',
            'AttributeType': 'S'
        }
    ],
    TableName='students',
    KeySchema=[
        {
            'AttributeName': 'student_name',
            'KeyType': 'HASH'
        },
{
            'AttributeName': 'student_id',
            'KeyType': 'RANGE'
        },
    ],
    LocalSecondaryIndexes=[
        {
            'IndexName': 'course_id',
            'KeySchema': [
                {
                    'AttributeName': 'course_id',
                    'KeyType': 'RANGE'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            }
        },
    ],
    BillingMode='PAY_PER_REQUEST',

)

1 Answer 1

1

You should explicitly specify the HASH key for the LSI too.

import boto3

client = boto3.client('dynamodb')

response = client.create_table(
    AttributeDefinitions=[
        {
            'AttributeName': 'student_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'student_name',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'course_id',
            'AttributeType': 'S'
        }
    ],
    TableName='students',
    KeySchema=[
        {
            'AttributeName': 'student_name',
            'KeyType': 'HASH'
        },
        {
            'AttributeName': 'student_id',
            'KeyType': 'RANGE'
        },
    ],
    LocalSecondaryIndexes=[
        {
            'IndexName': 'course_id',
            'KeySchema': [
                {
                    'AttributeName': 'student_name',
                    'KeyType': 'HASH'
                },
                {
                    'AttributeName': 'course_id',
                    'KeyType': 'RANGE'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            }
        },
    ],
    BillingMode='PAY_PER_REQUEST',

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.