owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
2.94k stars 382 forks source link

Prepared statement contains too many placeholders #946

Closed rebbieboi closed 3 weeks ago

rebbieboi commented 3 weeks ago

PHP Version

8.1.28

Laravel Version

10.39.0

Package Version

13.6.4

Description

Audit is triggering a delete and in doing so logs with the error below. Yes, there is a lot of IDs sent to it for the IN condition

Prepared statement contains too many placeholders
(Connection: mysql, SQL: delete from `audits` where `audits`.`auditable_type` = App\Models\TheModel and `audits`.`auditable_id` = 4 and `audits`.`auditable_id` is not null and `id` in (

#0 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(776): Illuminate\\Database\\Connection->runQueryCallback()
#1 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(612): Illuminate\\Database\\Connection->run()
#2 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(557): Illuminate\\Database\\Connection->affectingStatement()
#3 /vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(3617): Illuminate\\Database\\Connection->delete()
#4 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1271): Illuminate\\Database\\Query\\Builder->delete()
#5 /vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23): Illuminate\\Database\\Eloquent\\Builder->delete()
#6 /vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(52): Illuminate\\Database\\Eloquent\\Relations\\Relation->forwardCallTo()
#7 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(517): Illuminate\\Database\\Eloquent\\Relations\\Relation->forwardDecoratedCallTo()
#8 /vendor/owen-it/laravel-auditing/src/Drivers/Database.php(37): Illuminate\\Database\\Eloquent\\Relations\\Relation->__call()
#9 /vendor/owen-it/laravel-auditing/src/Auditor.php(88): OwenIt\\Auditing\\Drivers\\Database->prune()
#10 /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(355): OwenIt\\Auditing\\Auditor->execute()
#11 /vendor/owen-it/laravel-auditing/src/AuditableObserver.php(110): Illuminate\\Support\\Facades\\Facade::__callStatic()
#12 /vendor/owen-it/laravel-auditing/src/AuditableObserver.php(55): OwenIt\\Auditing\\AuditableObserver->dispatchAudit()
#13 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(478): OwenIt\\Auditing\\AuditableObserver->updated()
#14 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(286): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}()
#15 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(266): Illuminate\\Events\\Dispatcher->invokeListeners()
#16 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(189): Illuminate\\Events\\Dispatcher->dispatch()
#17 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1217): Illuminate\\Database\\Eloquent\\Model->fireModelEvent()
#18 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1130): Illuminate\\Database\\Eloquent\\Model->performUpdate()
#19 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(990): Illuminate\\Database\\Eloquent\\Model->save()

Steps To Reproduce

not sure what triggers the delete

Possible Solutions

No response

willpower232 commented 3 weeks ago

Are you able to include the rest of the stack trace for some more context? At least the lines before it reaches your app.

rebbieboi commented 3 weeks ago

Are you able to include the rest of the stack trace for some more context? At least the lines before it reaches your app.

updated the post with the data requested

willpower232 commented 3 weeks ago

Thanks, are you able to confirm how many audit entries for this model you have?

rebbieboi commented 3 weeks ago

Thanks, are you able to confirm how many audit entries for this model you have?

afraid I don't know how to query the 'auditable_type' column since doing like below doesn't result with anything

SELECT count(id) FROM `audits` WHERE `auditable_id` = 4 and `auditable_type` =\ 'App\Models\TheModel\';

and this fails:

SELECT count(id) FROM `audits` WHERE `auditable_id` = 4 and `auditable_type` = App\Models\TheModel;

rebbieboi commented 3 weeks ago

Thanks, are you able to confirm how many audit entries for this model you have?

EDIT: it was 74k records, I manually pruned it instead as I remember you can just use LIKE to filter using auditable_type. However, I still hope that the package can handle more records when it's deleting them automatically.

willpower232 commented 3 weeks ago

Interesting, I'll leave it for @erikn69 to investigate the specifics.

The SQL needs a double slash so

SELECT count(id) FROM `audits` WHERE `auditable_id` = 4 AND `auditable_type` = 'App\\Models\\TheModel'
rebbieboi commented 3 weeks ago

Interesting, I'll leave it for @erikn69 to investigate the specifics.

The SQL needs a double slash so

SELECT count(id) FROM `audits` WHERE `auditable_id` = 4 AND `auditable_type` = 'App\\Models\\TheModel'

thank you for that! also, I set it as 'threshold' => 2500, in the config

erikn69 commented 3 weeks ago

13.6.4

In my opinion this was already solved in later versions, I removed the whereIn: #933/files Try v13.6.7


I'll leave it for @erikn69 to investigate the specifics.

Sorry, I can't do it at all and most of them aren't real bugs, it's a waste of time chasing ghosts

Also, take some time to review the code, I have noticed that you have not caught up yet Open a discussion if you don't understant something, and I will try to explain it to you, I have only contributed out of necessity, but the code is really small, and it is not complex

rebbieboi commented 3 weeks ago

13.6.4

In my opinion this was already solved in later versions, I removed the whereIn: #933/files Try v13.6.7

I'll leave it for @erikn69 to investigate the specifics.

Sorry, I can't do it at all and most of them aren't real bugs, it's a waste of time chasing ghosts

Also, take some time to review the code, I have noticed that you have not caught up yet Open a discussion if you don't understant something, and I will try to explain it to you, I have only contributed out of necessity, but the code is really small, and it is not complex

aye, looks like that version does fix that problem. thanks!

willpower232 commented 3 weeks ago

@erikn69 thanks for confirming, I only tagged you because I knew you had been in that part of the code very recently and would know what the issue was straight away :pray: