Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key: with number type -> type mismatch #596

Open
Matan-Perry opened this issue Oct 24, 2022 · 7 comments
Open

key: with number type -> type mismatch #596

Matan-Perry opened this issue Oct 24, 2022 · 7 comments

Comments

@Matan-Perry
Copy link

Matan-Perry commented Oct 24, 2022

Hi!

I am trying to define a table with an integer as the key.

  class Model
    include Dynamoid::Document

    table name: "table_name", key: :my_key, capacity_mode: :on_demand

    field :my_field, :integer
  end

In my schema i define my_key to be the partition key. when my_key is a string everything work's great, but when i change the schema to :

            attribute_definitions: [
              {
                attribute_name: "my_key",
                attribute_type: "N",  ## (Was S)
              },

I am getting :

     Aws::DynamoDB::Errors::ValidationException:
       One or more parameter values were invalid: Type mismatch for key

I couldn't find any example with a number type key. is this supported?

Thanks

@andrykonchin
Copy link
Member

andrykonchin commented Oct 25, 2022

Hi.

First of all - integer/numeric partition keys are supported.

Next. Integer field should be declared with :integer or :number type (not :boolean):

field :my_field, :integer

DynamoDB supports boolean type natively and AFAIK it isn't related or converted implicitly to integer.

Dynamoid supports also storing boolean values (boolean Ruby values - TrueClass/FalseClass) as a DynamoDB string attribute (f and t characters). It's a legacy functionality but it still exists and by default boolean fields are stored as a string attribute (https://github.com/Dynamoid/dynamoid#note-on-boolean-type). That's why in your example the boolean field is stored as a string and it conflicts with the declared partition key type (Number).

@Matan-Perry
Copy link
Author

Thanks for the reply. i've used this field as an example, its not the actual field - i edited it.
My question is, how do i tell Dynamoid that my_key is an integer? i keep getting a type mismatch error when passing an integer. when i modify and schema to have a type "S", it works, but only if i use that key as a string.

@andrykonchin
Copy link
Member

andrykonchin commented Oct 25, 2022

Could you plz provide a minimal example to reproduce the issue?

In the edited code snippet above I noticed that attribute names (used in table and field calls) are different - my_key and my_field.

Answering your question about declaration of the integer partition key. It should be declared as a field of :integer type. Exactly like you do in your example.

@Matan-Perry
Copy link
Author

Could you plz provide a minimal example to reproduce the issue?

In the edited code snippet above I noticed that attribute names (used in table and field calls) are different - my_key and my_field.

Answering your question about declaration of the integer partition key. It should be declared as a field of :integer type. Exactly like you do in your example.

Hey! When i define my_key as a key in the schema, do i need to also define it as a field? in the README example the key is not defined as a field. i added my_field as an example to make the snippet more clear, but i can reproduce is without any fields - just defining a table with a key, the same way as the read me, and trying to make that key an number (type "N") in the schema.

@andrykonchin
Copy link
Member

andrykonchin commented Oct 27, 2022

Hey! When i define my_key as a key in the schema, do i need to also define it as a field? in the README example the key is not defined as a field.

Yes, to have a non-String partition key a new field (for this attribute) should be declared explicitly with a needed type.

Example:

    class User
      include Dynamoid::Document

      table key: :numeric_key

      field :numeric_key, :integer
    end

@Matan-Perry
Copy link
Author

Matan-Perry commented Oct 28, 2022

Thanks !
Looks like that fixes it. does it worth adding an example in the README? or maybe, support for type (:string or :number) in the table definition?

@andrykonchin
Copy link
Member

Specifying a hash key type in the table definition is in my TODO list, heh. But it's definitely makes sense to add an example in Readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants