question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modifying columns

See original GitHub issue

I think it is a reasonable expectation that it would be possible to:

  • rename a column
  • change the data type
  • add or drop the null constraint
  • change the default

And probably other things that I have not thought of.

I would imagine the syntax for doing this would be something like

 knex.Schema.table('table_name', function (t) {
        t.string('my_column').renameTo('something_else');
        t.string('my_column').changeTo('text');
        t.string('my_column').nullable() < adds nullable if not already present
        t.string('my_column').notNullable() < removes nullable if present
        t.string('my_column').defaultTo('whatever') < adds or updates the default
  });

Maybe there needs to be something extra in the chain, possibly at the knex.Schema.table level to indicate that this is a modify statement not an add.

I realise that this is tricky to implement across various databases, especially in SQLite where you have to create a whole new table, but in a system which requires migrations, without these tools it is going to be necessary to use knex.raw and write all the migrations for each DB supported, which sort of defeats the point of having a nice ORM especially one which is about to support migrations.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:28
  • Comments:61 (42 by maintainers)

github_iconTop GitHub Comments

10reactions
tkellencommented, Apr 16, 2016

Switching between MySQL and PostgreSQL seamlessly is a pipe dream that will never happen for anyone on a meaningful scale ever. All database systems have strengths and weaknesses. It makes absolutely no sense to limit yourself to a subset that works across both so you can arbitrarily decide to switch between one or the other.

ActiveRecord doesn’t do migrations “right”. There is a huge set of functionality it doesn’t provide a DSL for. You are right that more human effort has been dumped into ActiveRecord’s migration system though. The reason for that is simple numbers.

Knex: screen shot 2016-04-16 at 10 44 59 am

ActiveRecord: screen shot 2016-04-16 at 10 44 33 am

My question remains. Where is the value in these DSLs? Anything you want to migrate can be expressed in SQL. You don’t need to build some half-baked sugar on top of it. People have been talking about altering columns here for 3 years. Meanwhile SQL has been trucking along doing the same thing just fine for 30.

Also, RE: switching between PostgreSQL and MySQL, what happens if you decide to move from javascript to ruby? How much time will it take you to transcribe all of your SQL migrations from one silly pointless DSL to another? Maybe we should write a portable DSL, you say? That’s SQL.

7reactions
RobertWarrenGilmorecommented, Mar 29, 2016

I would love to see an API like table.modifyColumn('myCol').notNullable().defaultTo('hello').

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel 2010: Modifying Columns, Rows, and Cells - GCF Global
Select the columns you want to modify. Click the Format command on the Home tab. The format drop-down menu appears. Select Column Width....
Read more >
Modifying Columns, Rows, and Cells - Excel Tutorial
To Modify Column Width: ... Click and drag the column to the right to increase the column width or to the left to...
Read more >
Modifying Columns in DataFrame
Modifying Columns in DataFrame¶ · Rename columns · Add columns · Delete columns · Insert/Rearrange columns · Replace column contents ...
Read more >
Modifying Columns, Rows, and Cells - Ethel M. Rendon ...
1. Select the columns you want to modify. · 2. Click the Format command on the Home tab. The format drop-down menu appears....
Read more >
26 Creating and modifying columns | R for Epidemiology
Therefore, I'm going to introduce you to using bracket notation to create and modify data frame columns now. The mutate() function from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found