baopham / laravel-dynamodb

Eloquent syntax for DynamoDB
https://packagist.org/packages/baopham/dynamodb
MIT License
484 stars 128 forks source link

Multi condition with range and primary key scan #237

Closed andrenjdev closed 3 years ago

andrenjdev commented 3 years ago

Describe the bug

This is my model: class DashboardProfileDataDDB extends DynamoDbModel { // protected $table = 'profile-dashboard'; protected $guarded = []; protected $primaryKey = ['instagram_id']; protected $compositeKey = ['date']; }

This is my Model Query:

$current_profile_info = DashboardProfileDataDDB::where('instagram_id', $request->instagram_id)->where('date', 'begins_with', $date)->get();

This is a Manual Query: $current_profile_info = DynamoDb::table('profile-dashboard') ->setKeyConditionExpression('#name = :instagram_id and begins_with(#date, :date)') //->setProjectionExpression('ALL') ->setExpressionAttributeName('#name', 'instagram_id') ->setExpressionAttributeName('#date', 'date') ->setExpressionAttributeValue(':instagram_id', DynamoDb::marshalValue($request->instagram_id)) ->setExpressionAttributeValue(':date', DynamoDb::marshalValue($date)) ->prepare() ->query();

I can't understand why with Model query it makes a scan otherwise with manual query it works well with. How I have to set double condition with Primary and Range key?

Thank you

Debug Info

$model = new DashboardProfileDataDDB(); $query = $model->newQuery() ->where('instagram_id', $request->instagram_id) ->where('date', 'begins_with', $date); $raw = $query->toDynamoDbQuery(); dump($raw->op); dump($raw->query);

^ "Scan" ^ array:4 [▼ "TableName" => "profile-dashboard" "FilterExpression" => "#instagram_id = :a1 AND begins_with(#date, :a2)" "ExpressionAttributeNames" => array:2 [▼ "#instagram_id" => "instagram_id" "#date" => "date" ] "ExpressionAttributeValues" => array:2 [▼ ":a1" => array:1 [▼ "S" => "17841400509490519" ] ":a2" => array:1 [▼ "S" => "2021-02-26T" ] ] ]

Version info

andrenjdev commented 3 years ago

SOLVED :) I added my primary key as composite key! protected $compositeKey = ['instagram_id','date'];