linq2dynamodb icon indicating copy to clipboard operation
linq2dynamodb copied to clipboard

Object property cannot have >1 DynamoDBPropertyAttribute

Open howcheng opened this issue 9 years ago • 2 comments

If a property is part of the primary key and also an index, there's an exception thrown when trying to determine the property converter.

Example:

[DynamoDBHashKey("ColumnName", typeof(My.Project.Converter)]
[DynamoDBGlobalSecondaryIndexHashKey("My-index-name", Converter = typeof(My.Project.Converter)]
public int MyProperty { get; set; }

The error is in DynamoDbConversionUtils.GetPropertyConverter():

var propertyAttribute = propInfo.GetCustomAttributes(typeof(DynamoDBAttribute), true)
      .OfType<DynamoDBPropertyAttribute>()
      .SingleOrDefault();

howcheng avatar Mar 10 '16 18:03 howcheng

Hi Howard,

Well, I'm not sure this would work even if you marked your property with just one of those attributes. Linq2DynamoDB doesn't require and doesn't support neither DynamoDBHashKey nor DynamoDBGlobalSecondaryIndexHashKey.

Are you sure you really need them? What for?

scale-tone avatar Mar 11 '16 14:03 scale-tone

I had a table where one attribute was the table hash key as well as the hash key for a global secondary index. I tried commenting out the DynamoDBGlobalSecondaryIndexHashKey attribute, and instead got some other error (which I didn't note at the time, sorry, but I think it had do with DynamoDB not able to tell if I was querying using the primary key or the GSI). In the end, it turned out I didn't need that GSI, so we just dropped it, completely side-stepping this issue.

Still, you could fix the bug by finding the first DynamoDBAttribute that has a converter defined, since it doesn't make sense that a column would require two different converters.

howcheng avatar Mar 18 '16 21:03 howcheng