1

I'm following a tutorial for node sequelize mysql, but I'm using postgresql. From what I read, sequelize package will automatically turn autoincrement true into Serial data type in postgres dialect, but for me it keeps executing "id" INTEGER.

This is how I initialize sequelize

const Sequelize = require('sequelize')

const sequelize = new Sequelize('my-database-name', 'postgres', 'mypassword', {
    dialect: 'postgres',
    protocol: 'postgres',
    host: 'localhost',
    port: '5432',
})

module.exports = sequelize

This is how I initialize my model

const Sequelize = require('sequelize')

const sequelize = require('../util/database')

const Product = sequelize.define('product', {
    id: {
        type: Sequelize.INTEGER,
        autoincrement: true,
        primaryKey: true,
    },
    title: Sequelize.STRING,
    price: {
        type: Sequelize.DOUBLE,
        allownull: false,
    },
    imageUrl: {
        type: Sequelize.STRING,
        allownull: false,
    },
    description: {
        type: Sequelize.STRING,
        allownull: false,
    },
})

module.exports = Product

This is my app.js where I run the server

const path = require('path')

const express = require('express')
const bodyParser = require('body-parser')

const errorController = require('./controllers/error')
const sequelize = require('./util/database')

const app = express()

app.set('view engine', 'pug')
app.set('views', 'views')

const adminRoutes = require('./routes/admin')
const shopRoutes = require('./routes/shop')

app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'public')))

app.use('/admin', adminRoutes)
app.use(shopRoutes)

app.use(errorController.get404)

sequelize
    .sync()
    .then(result => {
        // console.log(result)
        app.listen(3000)
    })
    .catch(err => console.log(err))

This is what I get from the terminal

[nodemon] starting `node app.js`
Executing (default): CREATE TABLE IF NOT EXISTS "products" ("id" INTEGER , "title" VARCHAR(255), "price" DOUBLE PRECISION, "imageUrl" VARCHAR(255), "description" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH 
TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'products' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;

I'm hoping to get this execute

Executing (default): CREATE TABLE IF NOT EXISTS "products" ("id" SERIAL PRIMARY KEY...

Because I get this error whenever I add new record

Executing (default): INSERT INTO "products" ("id","title","price","imageUrl","description","createdAt","updatedAt") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *;
DatabaseError [SequelizeDatabaseError]: null value in column "id" violates not-null constraint

I have instal --save the pg and sequelize package. Here is my package.json

{
...
  "devDependencies": {
    "nodemon": "^1.18.3"
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "ejs": "^3.0.1",
    "express": "^4.16.3",
    "express-handlebars": "^3.1.0",
    "pg": "^7.17.1",
    "pug": "^2.0.4",
    "sequelize": "^5.21.3"
  }
}

1 Answer 1

1

You don't need to add the id as part of your model, sequelize helps handle this part by itself.


const sequelize = require('../util/database')

const Product = sequelize.define('product', {
    title: Sequelize.STRING,
    price: {
        type: Sequelize.DOUBLE,
        allownull: false,
    },
    imageUrl: {
        type: Sequelize.STRING,
        allownull: false,
    },
    description: {
        type: Sequelize.STRING,
        allownull: false,
    },
})

module.exports = Product

Keep it like this, it will handle itself

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.