Search code examples
c#servicestackamazon-dynamodbpocodynamo

How create a QueryExpression with null field in PocoDynamo


I want query over GSI when a range key is null. I have this code, but it thrown a exception

IPocoDynamo dbDynamo = new PocoDynamo(new AmazonDynamoDBClient());
var queryExpression = dbDynamo.FromQueryIndex<IndexName>(x => x.InvalidFrom == (DateTime?)null);
var response = dbDynamo.Query(queryExpression);

My model looks like this

[References(typeof(IndexName))]
[Alias("TableName")]
public class Child
{
   [AutoIncrement]
   public int ChildId { get; set; }

   public int ParentId { get; set; }
   public string Key { get; set; }
   public DateTime? InvalidFrom { get; set; }
   public decimal Value { get; set; }
}

This is my index

public class IndexName: IGlobalIndex<Child>
{
   [HashKey]
   public int ParentId { get; set; }

   [RangeKey]
   public DateTime? InvalidFrom { get; set; }

   public int ChildId { get; set; }
}

What i am doing wrong ?

Thanks


Solution

  • Firstly when you use a query you always need to supply the Hash in the Query, so it needs a minimum of:

    var q = dbDynamo.FromQueryIndex<IndexName>(x => 
        x.ParentId = parentId && x.InvalidFrom == (DateTime?)null);
    

    But trying to insert an item without a InvalidFrom value, e.g:

    db.PutItem(new Child { ParentId = 2, Key = "Key2", Value = 2 });
    

    Will succeed when Child doesn't have an Global Index, but will fail when it has a IndexName containing a nullable DateTime? Range Key with:

    Invalid attribute value type
    

    Essentially AWS wont let you insert a NULL value when it's defined on a Global Index Range Key, but it will let you insert a NULL value on a non-RangeKey property. So this use-case isn't supported.