A user, pacharanero, reported a bug where they were unable to recreate a deleted chat channel with the same name due to a 500 error caused by a unique constraint on the slug. This issue was previously reported but seemed to persist despite being on the latest version.
Mcwumbly suggested a workaround, but it did not work for pacharanero. Joffreyjaffeux explained that channels are not deleted but soft-deleted, and the issue might be due to a slug not being deleted properly.
Martin provided a solution using the Rails console, suggesting to use with_deleted to find the deleted channel and update its slug. This solution worked for pacharanero.
Later, MarcP reported a similar issue and tried the provided workaround, but it failed due to a namespacing issue introduced weeks ago, where ChatChannel is now Chat::Channel. MarcP eventually solved the issue by updating the slug directly in the database. Joffreyjaffeux confirmed that this workaround should be good.
This bug was reported in the linked thread but seems to still persist for me. I’m on latest and did a git pull and launcher rebuild yesterday.
When trying to name a new chat channel after any previously used chat channel name, there is a 500 error in the JS console.
In the Discourse error logs I get
Failed to handle exception in exception app middleware : ActiveRecord::RecordNotUnique : PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_chat_channels_on_slug"
DETAIL: Key (slug)=(sec-fhir) already exists.
In the Rails console I can see that the deleted ChatChannels have gone, but I suspect somewhere the deleted slug remains in an index or in some other DB dependency and hasn’t been deleted.
This workaround doesn’t work for me, upon trying to rename “foo2” to “foo” I get the same 500 error, because of the failed uniqueness constraint on the slug.
I’m not sure how to safely delete the (indexed?) slugs of deleted ChatChannels, but I’d be willing to have a go.
Create channel with name “whiskers” and slug “whiskers”
Delete channel
Create channel with name “whiskers” and slug “whiskers”
That worked.
When the channel is deleted (step 2), the slug is automatically changed to something unique. But since you deleted your channel prior to that change being introduced, the old slug may be still hanging around.
You can probably manually update the slug for the deleted channel via the rails console, but I’m not sure what the right incantation is. I’ll ask around.
Note also, that channel slugs are now able to be edited independently of the channel name, so as a workaround, you could also make a channel with the desired name today and choose a different slug that doesn’t conflict.
I can confirm that this test also works for me on my instance, which is fully up to date.
I think the issue is that somewhere in the DB there is some slug hanging around that should have been deleted, which wasn’t, maybe there was a bug in a previous version which is now fixed. Chat has only been enabled on this instance for 17 days, so any bug would have to have been active in that time-frame.
Can’t repro this in any way. After doing a DB backup maybe you can try these in the rails console. Seems surprising though.
[1] pry(main)> DB.exec("DROP INDEX IF EXISTS index_chat_channels_on_slug;")
[2] pry(main)> DB.exec("CREATE UNIQUE INDEX index_chat_channels_on_slug ON chat_channels(slug);")
Thanks for the above. I didn’t know about with_deleted but that enabled me to see the deleted instances and to rename their slugs as per your snippet.
I can confirm this is fixed. I would agree with @j.jaffeux that this is not likely to be affecting many users so I’d say it’s enough that a Rails console fix is here in the thread.