go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

Issue with `Preload(...)` and Prepared Statements After Schema Migration #7255

Open jared-mackey opened 4 weeks ago

jared-mackey commented 4 weeks ago

Description

Hi,

First, thank you for all the hard work you do on GORM!

I’m encountering an issue when using Preload("Blah.Blah") in combination with prepared statements. In our setup, we deploy multiple instances of our application and perform rolling deployments. When deploying a new version that includes a migration, the migration is applied before starting the new pods, which function as expected.

However, this causes issues for the old pods that are still running and handling traffic. Specifically, if the migration adds a new column to a table, it invalidates the prepared statement caches in the old pods that use SELECT * generated from the Preload() calls on the affected tables. As a result, the old pods break with the error cached plan must not change result type (SQLSTATE 0A000), while the new ones are fine.

Proposal/Question: Would it be possible for Preload(...) to inspect the struct’s fields and generate a query that selects only those fields, instead of using SELECT *? This way, the old pods would continue to have valid query caches while the new pods begin selecting the additional fields introduced by the migration / changes to the structs.

Thanks again!