Why aren't there more foreign keys in the Discourse database?

I saw discourse db table schema only has several foreign keys, is there any reason to not use the foreign keys in most tables?

1 Like

I come to the forum to ask the exact same question after looking at the tables.

In my limited knowledge, the constraints probably might have been handled on Rails level with models. But I would like to know more in this regard.

1 Like

We are thinking about this problem and have not come to a clear cut decision yet.

Some of us want absolutely all foreign keys defined in PG and think the benefit far outweigh the risk here. Other feel that making a drastic change like this now would be risky.

Foreign keys do introduce constraints on the database, if for example category_users table had a constraint saying category_id has to be a valid id in the categories table, every time we insert stuff into category_users we would have to check that, it adds some cost and we have lots and lots of dependencies going on.

Anyway… this is a concern we are aware of.