kurtlibby
kurtlibby4mo ago

Migrating a DB and Relationships

I'm looking at the import options and seeing that we can't include _id in the objects unless we have a Snapshot. See here: https://docs.convex.dev/database/import-export/import#features I'm totally fine with Convex creating new _ids, but I don't understand how relationships can be maintained when migrating from another service where I don't have the Snapshot. Since I'm migrating from MongoDB and I've got production data, it's unclear how to maintain those relationships. For instance, if a user has a list of tasks, the Task object has a user property with the _id of the user OR the User object has an array called tasks that has the _id for all of the users tasks. Either way, it is unclear how to preserve those relationships on import. Anyone know how to do this or a good resource for understanding the process?
5 Replies
Matt Luo
Matt Luo4mo ago
The first example sounds like a straightforward foreign key which you define in schema.ts Convex has an array type for the column, but I believe there are limitations With regard to the size of the array. The limitations are in the docs
ian
ian4mo ago
You can temporarily have a separate id field with an index on it, import things, then run a migration where you look up the object by id, then replace it with the _id. The field type could be v.union(v.string(), v.id(…))
ballingt
ballingt4mo ago
@kurtlibby note there's no performance penalty for using a different column as the foreign key join column as long as you add an index. _id gets some special treatment in APIs like ctx.db.get(id) but you're not limited to using that column for foreign keys.
kurtlibby
kurtlibbyOP4mo ago
I was able to get a sample set of data migrated over and did a migration from the mongo _id to the convex _id. not too difficult. Thanks for the pointers everyone!
jamalsoueidan
jamalsoueidan4mo ago
@rgz

Did you find this page helpful?