PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

TTL update on table create fails if table already exists

Open ryancausey opened this issue 6 months ago • 2 comments

I'm running into an issue where when calling Model.create_table when a table already exists leads to the error: "pynamodb.exceptions.TableError: Failed to update TTL on table: An error occurred (ValidationException) on request (asdf) on table (asdf) when calling the UpdateTimeToLive operation: TimeToLive is already enabled".

This appears to be due to the if not cls.exists() check not gating the call to cls.update_ttl. I can pass ignore_update_ttl_errors to ignore this, but I'm using DynamoDB local which supports TTLs. It seems like there is a mismatch in the code behavior. Should cls.update_ttl only be called if the table doesn't already exist and is being created?

ryancausey avatar Jun 14 '25 05:06 ryancausey

This makes sense to me.

I'll also call out (docs should probably mention this too) that pynamodb's create_table etc. are useful for local stacks but any production use should probably be done with proper DevOps tooling like Cloudformation, Terraform and the likes.

ikonst avatar Jun 15 '25 22:06 ikonst

I agree with the dev ops tooling note. My actual tables are managed by Terraform, but when I'm running tests locally or in CI I'm using DynamoDB local. I'm also using some Factoryboy and pytest fixtures I wrote to populate test data and delete/recreate the tables after each test in DynamoDB local using Model.delete_table and Model.create_table. That's how I ran into this.

I'll open a PR for this change.

ryancausey avatar Jun 16 '25 04:06 ryancausey

This is actually a bit more complicated due to the fact that wait is not true by default. This means any attempt to update the TTL without waiting could also cause the update TTL call to fail. Attempting to add this would likely be extra code without any meaningful improvement in behavior. I'm just going to close this.

ryancausey avatar Jul 02 '25 21:07 ryancausey