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.

Make an existing nullable column non nullable

See original GitHub issue

i have an existing column which ive created, The column has some data in it. How do i make it non nullable, still preserving the data.

 .table('users', function(table){
    table.string('mobile').notNull();
  })

This attempts to add a new column.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

59reactions
etcommented, Feb 20, 2018

For those of you coming here from google, the syntax is:

Converting an existing nonNullable column to be nullable (reverse the up/down if you want a nonNullable to be nullable).

exports.up = knex => {
  return knex.schema
    .alterTable('images', (table) => {
      table.string('checksum').nullable().alter();
    });
};

exports.down = knex => {
  return knex.schema
    .alterTable('images', table => {
      table.string('checksum').notNullable().alter();
    });
};
0reactions
marcel099commented, Oct 3, 2022

For those of you coming here from google, the syntax is:

Converting an existing nonNullable column to be nullable (reverse the up/down if you want a nonNullable to be nullable).

exports.up = knex => {
  return knex.schema
    .alterTable('images', (table) => {
      table.string('checksum').nullable().alter();
    });
};

exports.down = knex => {
  return knex.schema
    .alterTable('images', table => {
      table.string('checksum').notNullable().alter();
    });
};

It’s worth mentioning that the .nullable().alter() methods didn’t work for me because my personal project uses SQLIte and it does not support alter column.

Here’s de error message:

migration file "05_alter_users_drop_nullability_of_avatar.ts" failed
migration failed with error: Sqlite does not support alter column.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Altering a Column from Null to Not Null in SQL Server - Chartio
Most critically, all existing NULL values within the column must be updated to a non-null value before the ALTER command can be successfully...
Read more >
Altering a column: null to not null - sql server - Stack Overflow
SET MyNullableColumn = 0 only works if your existing column is a numeric column. For non-numeric types you need to set another default...
Read more >
Problems with adding NOT NULL columns or making nullable ...
In the first case, you simply add a DEFAULT constraint to the column first, with a value that isn't NULL , and in...
Read more >
SQL NOT NULL Constraint - W3Schools
By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a...
Read more >
SQL SERVER - Altering Column - From NULL to NOT NULL
Step 1: Convert all values to NOT NULL · Step 2: Change Column NULL Values ...
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