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

Accessors break query keys #232

Open
alex-french opened this issue Nov 16, 2020 · 0 comments
Open

Accessors break query keys #232

alex-french opened this issue Nov 16, 2020 · 0 comments

Comments

@alex-french
Copy link

alex-french commented Nov 16, 2020

Describe the bug

Where an accessor exists on a partition key, the key is passed through the accessor prior to query being executed, resulting in queries that can be incorrect.

Schema

Primary key: siteSensor (string)
Partition key: dateFrom (string)
No GSI

In the Eloquent model:

protected $primaryKey = 'siteSensor';
protected $compositeKey = ['siteSensor', 'dateFrom'];

I also use typical date mutator/accessor to convert from European dd/mm/YYYY to/from YYYY-mm-dd

        // Date mutators/accessors
	public function getDateFromAttribute($value)
	{
		return ( $value ? date('d/m/Y', strtotime($value) )  : null);
	}
	
	public function setDateFromAttribute($value)
	{
		$value = str_replace('/', '-', $value);
		$this->attributes['dateFrom'] = $value ? date('Y-m-d', strtotime($value)) : null;
	}

Debug info

Show the query that you're having trouble with by copy-pasting the result of:

"TableName" => "EnergyConversionFactorsDev"
    "KeyConditionExpression" => "#siteSensor = :a1 AND #dateFrom = :a2"
    "ExpressionAttributeNames" => array:2 [
      "#siteSensor" => "siteSensor"
      "#dateFrom" => "dateFrom"
    ]
    "ExpressionAttributeValues" => array:2 [
      ":a1" => array:1 [
        "S" => "aaaaaa#111"
      ]
      ":a2" => array:1 [
        "S" => "01/11/2020"
      ]
    ]

Note that the date string is being passed into the mutator first, then back to the accessor. Changing the date input string to 2020-11-01 does not make a difference since it still gets fed to the accessor.

Throwing a stack trace in the accessor shows that at vendor/baopham/dynamodb/src/DynamoDbModel.php:386 the following call is made, which subsequently passed the values throw the accessors:

$key[$name] = $this->getAttribute($name);
Version info

  • Laravel: 6.18.13
  • laravel-dynamodb: 6.1.1 (also exists in 6.1.0)
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

1 participant