LoginSignup
0
0

More than 1 year has passed since last update.

[Python3 / pynamodb] create_table時に Table read and write throughput must be at least 1, and cannot be null

Posted at

発生したエラー

E pynamodb.exceptions.TableError: Failed to create table: An error occurred (ValidationException) on request (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) on table (some_data) when calling the CreateTable operation: Table read and write throughput must be at least 1, and cannot be null

.venv/lib/python3.8/site-packages/pynamodb/connection/base.py:651: TableError

Table read and write throughput must be at least 1, and cannot be nullがエラーの原因っぽいことはすぐにわかった。
けれど、Table read and write throughputをどうやって設定するのかが一見してわからない。

この対処法を本記事に記載する。

環境

ソースコード

ダメなやつ
from pynamodb.models import Model


class SomeModel(Model):
    class Meta:
        table_name = "some_data"
        region = "us-east-1"

    some_id: UnicodeAttribute = UnicodeAttribute(hash_key=True)
    user_id: UnicodeAttribute = UnicodeAttribute()


def create_some_table():
    model = SomeModel
    model_meta_class = getattr(SomeModel, "Meta")
    setattr(model_meta_class, "host", "http://localhost:8000")
    setattr(model_meta_class, "region", "ap-northeast-1")
    setattr(model_meta_class, "table_name", "some_data")
    setattr(model_meta_class, "aws_access_key_id", "dummy")
    setattr(model_meta_class, "aws_secret_access_key", "dummy")

    model.create_table(wait=True)


create_some_table()

このソースコードのままcreate_some_tableを実行すると、以下のようなエラーが出る

E pynamodb.exceptions.TableError: Failed to create table: An error occurred (ValidationException) on request (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) on table (some_data) when calling the CreateTable operation: Table read and write throughput must be at least 1, and cannot be null

.venv/lib/python3.8/site-packages/pynamodb/connection/base.py:651: TableError

対処法

以下のように、setattr(model_meta_class, "billing_mode", "PAY_PER_REQUEST")を追加したら直った。

対処後
from pynamodb.models import Model


class SomeModel(Model):
    class Meta:
        table_name = "some_data"
        region = "us-east-1"

    some_id: UnicodeAttribute = UnicodeAttribute(hash_key=True)
    user_id: UnicodeAttribute = UnicodeAttribute()


def create_some_table():
    model = SomeModel
    model_meta_class = getattr(SomeModel, "Meta")
    setattr(model_meta_class, "host", "http://localhost:8000")
    setattr(model_meta_class, "billing_mode", "PAY_PER_REQUEST")  # <- 追加
    setattr(model_meta_class, "region", "ap-northeast-1")
    setattr(model_meta_class, "table_name", "some_data")
    setattr(model_meta_class, "aws_access_key_id", "dummy")
    setattr(model_meta_class, "aws_secret_access_key", "dummy")

    model.create_table(wait=True)


create_some_table()

これでOK。

Table read and write throughputというのは、billing_modeで指定できるものだったようだ。

なら、そう書いて欲しいぽよ...

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0