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

specify foreignKey #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

diligiant
Copy link

When it is not named after the owner’s type name plus its primary field name.

Fixes #18.

@davecheney
Copy link
Owner

Thank you for your PR. I'll have a close look at what indexes Gorm is generating by default and see if this helps.

@diligiant
Copy link
Author

You might find this interesting ; Here is what gorm does without and with "foreignKey:AdminID".

SELECT * FROM `actors` WHERE `actors`.`id` IN (\"110163848238256418\",\"110163851895700147\")
SELECT * FROM `accounts` WHERE `accounts`.`instance_id` = \"110163848239938280\"
SELECT * FROM `instance_rules` WHERE `instance_rules`.`instance_id` = \"110163848239938280\"
SELECT * FROM `instances` WHERE domain = \"example.com\" LIMIT 1

Here Preload(Admin) fetches all the actors (first admin, then me). I guess instance.Admin is me because that's the last loaded.

SELECT * FROM `actors` WHERE `actors`.`id` = \"110163848238256418\"
SELECT * FROM `accounts` WHERE `accounts`.`id` = \"110163848240493252\"
SELECT * FROM `instance_rules` WHERE `instance_rules`.`instance_id` = \"110163848239938280\"
SELECT * FROM `instances` WHERE domain = \"example.com\" LIMIT 1

Here Preload("Admin") only fetches the actor whose ID is AdminID

BTW, I also replaced Preload("Admin") by Joins("Admin") (you do this here) in the above cases: the first 3 calls are the same, the fourth one being replaced by something very long!

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

Successfully merging this pull request may close these issues.

instance.admin.(email for example) returns the last user created
2 participants