How do I deploy a change for a table name?
In the scenario where a table already has data in it, How do I deploy a change for a table name?
When I change the name of the table in my schema.ts, when I insert a new document, Convex is creating an entirely new table with the new name. The old table with the old name remains.
Kapa says "To rename a table, you would need to create a new table with the new name, migrate all the data from the old table to the new one, and then delete the old table."
https://discord.com/channels/1019350475847499849/1260308690490097664
9 Replies
Kapa is correct
Although if you need to maintain existing ids, you'll want to use import. Honestly not sure if or how that will actually work if you're trying to do import/export to the same db with a different table name, not sure it does.
So unless there's some workaround there, you'll need to update any tables that reference the table's id's as well.
Changing foreign key values sounds pretty difficult
Yeah, doable but tricky
If you're in production, more tricky
I have a number of tables that I would like to change the name of, but won't lol
It's hard because you'd have to maintain backups very carefully. An incorrect mass update needs a reliable rollback
You could also alias away your table names so the actual name is effectively a symbol that you don't see anywhere in your code.
if you really want to do this, the flow (to do what kapa and @erquhart suggested) would be:
1. snapshot export to download the current data as a zip file
2. modify the zip file in two ways: rename the directory representing the table, like
users/
to members/
, and rename the table on its line in _tables/documents.jsonl
3. npx convex import --replace
any modifications to the table in the meantime would be lost, and the export and import can be a bit slow, so this isn't a fantastic solution. i would test it on a dev deployment first if you're going to go with it, to make sure it works as expected.Okay thanks Lee. Looks like this flow keeps the primary key values
Can you say more about how to alias the table name? I'm looking at defineSchema() and defineTable() and can't figure out how to alias.
hide it in a file somewhere, export it, use it wherever the table name is needed
I'm sure there's a better way that would work with types, as intellisense would still prompt with the actual table name using this primitive approach.
Actually, would be cool to see that included in the convex lib at some point, ability to "rename" tables at the code level without actually renaming them in deployments. Convex as a platform treating table names as aliases could be a nice way of handling this overall, could even extend to the dashboard.
It's a good point. In most of my career, I would create tables using DDL, and make my ORM recognize the tables. But I think the spirit of Convex design is to use defineSchema() to create the table via
npx convex deploy
, so it would be helpful if Convex handled more of the responsibility of the table metadata