Why is SQL server throwing this error: Cannot insert the value NULL into column 'id'?

SqlSql Server

Sql Problem Overview


I'm using the following query:

INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())

However, I'm not specifying the primary key (which is id). So my questions is, why is sql server coming back with this error:

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'id', table 'CMT_DEV.dbo.role'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Sql Solutions


Solution 1 - Sql

I'm assuming that id is supposed to be an incrementing value.

You need to set this, or else if you have a non-nullable column, with no default value, if you provide no value it will error.

To set up auto-increment in SQL Server Management Studio:

  • Open your table in Design
  • Select your column and go to Column Properties
  • Under Indentity Specification, set (Is Identity)=Yes and Indentity Increment=1

Solution 2 - Sql

use IDENTITY(1,1) while creating the table eg

CREATE TABLE SAMPLE(
[Id]     [int]  IDENTITY(1,1) NOT NULL,
[Status] [smallint] NOT NULL,

CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)
)

Solution 3 - Sql

If the id column has no default value, but has NOT NULL constraint, then you have to provide a value yourself

INSERT INTO dbo.role (id, name, created) VALUES ('something', 'Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())

Solution 4 - Sql

if you can't or don't want to set the autoincrement property of the id, you can set value for the id for each row, like this:

INSERT INTO role (id, name, created)
SELECT 
      (select max(id) from role) + ROW_NUMBER() OVER (ORDER BY name)
    , name
    , created
FROM (
    VALUES 
	  ('Content Coordinator', GETDATE())
	, ('Content Viewer', GETDATE())
) AS x(name, created)

Solution 5 - Sql

You either need to specify an ID in the insert, or you need to configure the id column in the database to have Identity Specification = Yes.

Solution 6 - Sql

As id is PK it MUST be unique and not null. If you do not mention any field in the fields list for insert it'll be supposed to be null or default value. Set identity (i.e. autoincrement) for this field if you do not want to set it manualy every time.

Solution 7 - Sql

You need to set autoincrement property of id column to true when you create the table or you can alter your existing table to do this.

Solution 8 - Sql

you didn't give a value for id. Try this :

INSERT INTO role (id, name, created) VALUES ('example1','Content Coordinator', GETDATE()), ('example2', 'Content Viewer', GETDATE())

Or you can set the auto increment on id field, if you need the id value added automatically.

Solution 9 - Sql

I had a similar problem and upon looking into it, it was simply a field in the actual table missing id (id was empty/null) - meaning when you try to make the id field the primary key it will result in error because the table contains a row with null value for the primary key.

> This could be the fix if you see a temp table associated with the error. I was using SQL Server Management Studio.

Solution 10 - Sql

@curt is correct, but I've noticed that sometimes even this fails with NULL disallowed errors, and it seems to be intermittent. I avoided the error at all times, by also setting the Indenty Seed to 1 and IDENTITY(1, 1) NOT FOR REPLICATION.

Solution 11 - Sql

In my case,

I was trying to update my model by making a foreign key required, but the database had "null" data in it already in some columns from previously entered data. So every time i run update-database...i got the error.

I SOLVED it by manually deleting from the database all rows that had null in the column i was making required.

Solution 12 - Sql

if use entityframework. open migration, set value nullable: true, and update database

enter image description here

Solution 13 - Sql

You can insert a value manually in the ID column (here I call it "PK"):

insert into table1 (PK, var1, var2)
values ((select max(PK)+1 from table1), 123, 456)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - SqlCurtisView Answer on Stackoverflow
Solution 2 - SqlMinakshi KoradView Answer on Stackoverflow
Solution 3 - SqlAndy IrvingView Answer on Stackoverflow
Solution 4 - SqlrobotikView Answer on Stackoverflow
Solution 5 - SqlJupiterP5View Answer on Stackoverflow
Solution 6 - SqlquzaryView Answer on Stackoverflow
Solution 7 - SqlRisingDragonView Answer on Stackoverflow
Solution 8 - SqlnatadecocoView Answer on Stackoverflow
Solution 9 - Sqlvid.devView Answer on Stackoverflow
Solution 10 - SqlFandango68View Answer on Stackoverflow
Solution 11 - SqlJPTugirimanaView Answer on Stackoverflow
Solution 12 - Sqlchamskra66erView Answer on Stackoverflow
Solution 13 - SqlLømmisView Answer on Stackoverflow