Laravel-activitylog: Error when saving properties to database

Created on 16 Jan 2021  路  4Comments  路  Source: spatie/laravel-activitylog

Describe the bug
The user data contained a 馃檪 in one of the fields. When the model was saved, the following error occured:
Integrity constraint violation: 4025 CONSTRAINT properties failed for db_name.activity_log

So this is caused by the CHECK (json_valid(properties)) constraint. The smiley is not valid json, and probably should be encoded first.

I just removed the field from the logAttributes array as I don't really require the field to be logged. But maybe this helps improve this great package!

Versions (please complete the following information)

  • PHP: 7.2
  • Database: MariaDB
  • Laravel: 7
question

Most helpful comment

TLDR: you were right, it was a db issue. It only manifests in older version of MariaDB.

I did a full investigation of the problem:

  1. The smiley is stored in the actual table of the model. So it's not about db support.
  2. The issue was with MariaDB 10.2.15. I now also tested with MariaDB 10.3.2 and MariaDB 10.5.8. Both did not have this issue (I used a full DB dump).
  3. On MariaDB 10.2.15, I removed the constraint "CHECK (json_valid(properties))" and after that had no issues. The resulting record was exactly the same as in the other MariaDB versions.

Conclusion:
This probably was a bug in an older version of MariaDB in the json_valid function.

Just for reference, the statement not working in the old version of MariaDB was:
INSERT INTO activity_log (log_name, description, subject_type, subject_id, causer_type, causer_id, properties, created_at, updated_at) VALUES ('default', 'updated', 'test\Models\User', 99229, 'test\Models\User', 1, '{\"attributes\":{\"memo\":\"Have fun play fast \ud83d\ude42\"},\"old\":{\"memo\":\"old\"}}', '2021-01-10 17:26:59', '2021-01-10 17:26:59');

Result:
4025 - CONSTRAINT properties failed for xxxx.activity_log, Time: 0.031000s

Thanks for your time and effort!

All 4 comments

Without further debugging I would say that this is a bug in Laravel/Eloquent core as we don't serialize/encode the JSON ourselves but use the Eloquent cast logic to do so. 馃
I will add a testcase and check if we can reproduce it. Possibly it's also a problem with MariaDB or something similar.

Thanks!

I don't know how Laravel handles this internally, but according to PHP docs this should be used:
json_encode( $text, JSON_UNESCAPED_UNICODE );

I never used this myself, just from the docs. Maybe this helps.

Hey, after trying it my own and searching a bit it seems like your MariaDB setup isn't emoji compatible. 馃
Modern databases can handle emojis with and without escaping as long as the used character set allows it.
https://stackoverflow.com/questions/55437104/minimal-setup-for-mariadb-to-handle-emojis

Could you check this? As there's really nothing we could do in the package. And the Laravel core itself is also emoji compatible as long as the database is.

TLDR: you were right, it was a db issue. It only manifests in older version of MariaDB.

I did a full investigation of the problem:

  1. The smiley is stored in the actual table of the model. So it's not about db support.
  2. The issue was with MariaDB 10.2.15. I now also tested with MariaDB 10.3.2 and MariaDB 10.5.8. Both did not have this issue (I used a full DB dump).
  3. On MariaDB 10.2.15, I removed the constraint "CHECK (json_valid(properties))" and after that had no issues. The resulting record was exactly the same as in the other MariaDB versions.

Conclusion:
This probably was a bug in an older version of MariaDB in the json_valid function.

Just for reference, the statement not working in the old version of MariaDB was:
INSERT INTO activity_log (log_name, description, subject_type, subject_id, causer_type, causer_id, properties, created_at, updated_at) VALUES ('default', 'updated', 'test\Models\User', 99229, 'test\Models\User', 1, '{\"attributes\":{\"memo\":\"Have fun play fast \ud83d\ude42\"},\"old\":{\"memo\":\"old\"}}', '2021-01-10 17:26:59', '2021-01-10 17:26:59');

Result:
4025 - CONSTRAINT properties failed for xxxx.activity_log, Time: 0.031000s

Thanks for your time and effort!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

federico-arona picture federico-arona  路  5Comments

lflucasferreira picture lflucasferreira  路  3Comments

abdosaeedelhassan picture abdosaeedelhassan  路  4Comments

chriship picture chriship  路  4Comments

mtuchi picture mtuchi  路  3Comments