dannyelo
dannyelo6mo ago

How to remove (delete) a column in a table

Hi, I defined a column in my schema within a nextjs app and saw it in the data tab on the convex dashboard. I don't want that column anymore and want to remove it from the schema and the table, but now I got an error in the console.
Document with ID "......" in table "listings" does not match the schema: Object contains extra field `variants` that is not in the validator.
Document with ID "......" in table "listings" does not match the schema: Object contains extra field `variants` that is not in the validator.
Whats the procedure to achieve this? Thanks
3 Replies
erquhart
erquhart6mo ago
You'll need to set that field to undefined in every document on the table before removing it from the schema.
// convex/migrations.ts

import { makeMigration } from 'convex-helpers/server/migrations'
import { internalMutation } from './_generated/server'

const migration = makeMigration(internalMutation, {
migrationTable: 'migrations',
})

export const deleteVariantsField = migration({
table: 'listings',
migrateOne: async (ctx, doc) => {
await ctx.db.patch(doc._id, { variants: undefined })
},
})
// convex/migrations.ts

import { makeMigration } from 'convex-helpers/server/migrations'
import { internalMutation } from './_generated/server'

const migration = makeMigration(internalMutation, {
migrationTable: 'migrations',
})

export const deleteVariantsField = migration({
table: 'listings',
migrateOne: async (ctx, doc) => {
await ctx.db.patch(doc._id, { variants: undefined })
},
})
dannyelo
dannyeloOP6mo ago
Thanks
ian
ian6mo ago
Convex Developer Search
Search Docs, Stack, Discord all at once
Intro to Migrations
There are as many ways to migrate data as there are databases, but here’s some basic information to set the stage.
Lightweight Migrations
Patch all of your data in your database table with the bulk edit feature on the Convex dashboard, without writing migration code.
Stateful Migrations using Mutations
Online migrations in Convex using mutations. Including a helper to track migration state!

Did you find this page helpful?