pub icon indicating copy to clipboard operation
pub copied to clipboard

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

Open diligiant opened this issue 2 years ago • 7 comments

Hi,

That's probably a "gorm magic" issue and I really have no idea how to solve it. Check my test server (https://none.com/api/v1/instance/). You'll see that email, contact_account is myself although the serializer processes the i.Admin (admin@). When I check instances and accounts in Mariadb, this confirms that it holds the right info (see below).

Any hint/solution would be great.

thank you.

Frédéric

select admin_id from instances;
+--------------------+
| admin_id           |
+--------------------+
| 109694405584505771 |
+--------------------+
select id, actor_id, email from accounts;
+--------------------+--------------------+--------------------+
| id                 | actor_id           | email              |
+--------------------+--------------------+--------------------+
| 109694405584505771 | 109694405584200435 | [email protected]   |
select id,name from actors where id=109694405584200435;
+--------------------+-------+
| id                 | name  |
+--------------------+-------+
| 109694405584200435 | admin |
+--------------------+-------+

diligiant avatar Feb 07 '23 18:02 diligiant

thanks for spotting that; back in the day the instance admin was a total hack which was just finding a random account with a matching domain. I suspect that logic is still lurking around.

davecheney avatar Feb 07 '23 21:02 davecheney

I was under the impression the gorm magic wasn't working as before adding a LocalUser account, the correct info were returned but if you think that's "intentional", I'm gonna find this culprit ;)

diligiant avatar Feb 08 '23 00:02 diligiant

@davecheney I hope my PR will satisfy you ; I was really frustrated by this problem. I found 2 other occurrences (hoping I caught them all.)

Something puzzles me though: gorm documentation describes

type instance struct {
…
AdminID          *snowflake.ID
Admin            *Account
}

type Account {
snowflake.ID
}

as a Belongs To association, "the instance belongs to the admin account".

Shouldn't this be instead an Has One, "the instance has one admin account"? If so, one should take a look the the many associations in your schema.

my 2¢

diligiant avatar May 07 '23 23:05 diligiant

Damnit, did I break that again?

davecheney avatar May 08 '23 01:05 davecheney

It never worked (I found this when I first played with pub) as it never could (and I bet that's also true with Relationship & Status.) gorm is tricky (I almost rewrote with sqlc ;) here as its "convention" isn't "the previous field is named like the association field+primarykey". That's very subtle…

Regarding BelongsTo vs HasOne, I don't know if that changes anything today but isn't this a bet that gorm won't do things differently in the future?

diligiant avatar May 08 '23 07:05 diligiant

I get very confused by HasOne vs BelongsTo in gorm.

davecheney avatar May 08 '23 08:05 davecheney

you, me and a million people.

diligiant avatar May 08 '23 08:05 diligiant