Skip to content
Advertisement

Oracle not auto-generating primary key on insert

New to Oracle here. I have a table created with the following SQL:

create table Widgets (
    id              integer constraint pkWidgets primary key using index,
    ruleId          integer not null,
    customerId      integer constraint fkWidgets_Customers references Customers
);

I’m now trying to insert a record into this table with:

INSERT INTO Widgets (
  ruleId,
  customerId
) VALUES (
  88471239,
  null
);

And getting the following error:

INSERT INTO Widgets not successful
An error occurred when executing the SQL command:
INSERT INTO Widgets (
  ruleId,
  customerId...

ORA-01400: cannot insert NULL into ("MYSCHEMA"."WIDGETS"."ID") [SQL State=23000, DB Errorcode=1400]
1 statement failed.

Execution time: 0.13s

What’s going on here? Shouldn’t Oracle be auto-generating my primary key (id field) value? If not, how can I tell (exact SQL) what needs to be inserted for this value?

Advertisement

Answer

I was able to use ids.nextval and my insert works great:

INSERT INTO Widgets (
  id,
  ruleId,
  customerId
) VALUES (
  ids.nextval,
  88471239,
  null
);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement