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.

null value violates not-null constraint on id, postgres

See original GitHub issue

hi there.

attempting to create() a new instance of my menu object, as described here:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('menu', {
    title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    closed_view_title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    closed_view_photo_url: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    closed_view_message: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    soldout_view_title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    soldout_view_photo_url: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    soldout_view_message: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    max_items_per_order: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    starts_at: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    ends_at: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    service_boundary_url: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    service_boundary_updated: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  });
};

note: i have defined an id field previously, and removed it in an attempt to resolve this issue. sync() is being called on server start.

However, calling create() is yielding this error:

{"name":"SequelizeDatabaseError","message":"null value in column \"id\" violates not-null constraint"

Any ideas why sequelize is expecting me to add my own ID value? This seems to be an issue that has floated up several times, but none of the proposed solutions have been working (autoIncrement:true, removing the ID field entirely)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
ianseyercommented, Sep 3, 2015

Resolved.

My issue was having created the tables initially with ID having been set to Integer (this is because of using sequelize-auto). Therefore, any future changes I made to the table were not occuring because sync() does not touch tables if they already exist.

Therefore, I had to call sync({force: true}) to get it to rewrite my table schema.

0reactions
ianseyercommented, Aug 28, 2015

Sorry; it isn’t too large a project, though. Only a few routes defined and next to zero assets to pull down.

Getting it going should just be npm install; node app.js

Then navigate to /menus/create and fill in some garbage data.

The relevant files are /controllers/menus.js, /views/create.jade, /models/menu.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres error: null value in column "id" - during insert operation
Since you don't explicitly set it, it's implicitly given a null value, which is, of course, not a valid value for a primary...
Read more >
PostgreSQL Not-Null Constraint
We will introduce you to the NULL concept and how to use PostgreSQL not-null constraint to make sure that the values in columns...
Read more >
NotNullViolation: ERROR: null value in column "id" violates ...
After migrating from 11.0.4 source to 11.0.4 omnibus on a new server everything seemed to work fine. Then we went the long upgrade...
Read more >
PostgreSQL - NOT NULL Constraint - GeeksforGeeks
PostgreSQL Not-Null constraint as the name suggests is used to ensure that any value in the respective column is not null.
Read more >
An id of null throws violates not-null constraint. #142 - GitHub
Running the following command in psql gives me the same error: postgres=> INSERT INTO "User" (id, email, hash, role) postgres-> VALUES (null, ' ......
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