Skip to content
Discussion options

You must be logged in to vote

My understanding is alterTable, by default, can handle adding and removing columns (since removing has its own method(s)). The alter method comes into play when you need to actually alter an existing column.

So, for example, if I need to add a new column "summary" and alter an existing column called "body" from type varchar to text, my migration would look like this:

this.schema.alterTable(this.tableName, table => {
  table.string('summary') // 👈 adds new column
  table.text('body').alter() // 👈 changes type from varchar to text
})

Here's the documentation on alter() if you're interested:
https://knexjs.org/#Schema-alter

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by thetutlage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants