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

[BUG] Gorm keeps garbage associations in the database #7286

Open
droslean opened this issue Nov 21, 2024 · 2 comments
Open

[BUG] Gorm keeps garbage associations in the database #7286

droslean opened this issue Nov 21, 2024 · 2 comments
Assignees
Labels
type:critical critical questions

Comments

@droslean
Copy link

droslean commented Nov 21, 2024

Let's say we have

type Config struct {
	gorm.Model

	Params []Param
}

type Param struct {
	gorm.Model
	ConfigID uint

	Key   string
	Value string
}

Creating a config with specific params, gorm handles the associations normally, However, if we wish to update the params of the existing config, gorm will create a new params instead with the correct configID and it will clear the configID from the old params which technically create garbage in the database, since those entries are now useless and they will live as ghost for the rest of the eternity.

Trying to figure this out by reading https://gorm.io/docs/associations.html#Association-Mode, it seems that all operations related to associations are keeping garbage entries in the database.

This sounds like a BUG, or am I missing some critical configuration in the db session maybe?

@github-actions github-actions bot added the type:critical critical questions label Nov 21, 2024
@jinzhu
Copy link
Member

jinzhu commented Nov 21, 2024

Does the associations prefill the primary key when updating?

@droslean
Copy link
Author

image

@jinzhu A small example is attached. You can see the last entry in the active association of that config_id, and the previous one should have been deleted. Instead gorm, changes the config_id to null and keeps the entry. That will potentially create N of entries that will never be used anywhere.

If I add a new association this will happen to the existing data:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:critical critical questions
Projects
None yet
Development

No branches or pull requests

2 participants