pub icon indicating copy to clipboard operation
pub copied to clipboard

specify foreignKey

Open diligiant opened this issue 2 years ago • 2 comments

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

Fixes #18.

diligiant avatar May 07 '23 23:05 diligiant

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

davecheney avatar May 08 '23 01:05 davecheney

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!

diligiant avatar May 08 '23 13:05 diligiant