I have four tables:
users: id, thread_idthreads: id, language_idposts: id, user_id, language_idlanguages: id
USERS.thread_id is a foreign key to THREADS.id, POSTS.user_id is foreign key to USERS.id and POSTS.language_id foreign key to LANGUAGES.id.
I can’t delete an user because the POSTS.user_id will throw a foreign key constraint error, and I can’t delete the post because I want all the posts (and threads) to be readable there even if the user is deleted.
What should I do?
Looks like everything is working as designed 🙂 If MySQL let you delete the user while leaving posts.user_id pointing to it, your DB would become inconsistent.
If you want posts from deleted users to be readable, reset their user_id to something else (0? NULL?) before deleting the user.
If you want the user info to remain too, then you obviously can’t delete the user row. You should add some kind of ‘user is deleted’ column that changes how the user is shown on the UI.