gpt4 book ai didi

node.js - Node Sequelize postgres 方言不断为类型 : Sequelize. INTEGER 创建 INTEGER 类型而不是 SERIAL 类型的 id 和 autoincrement: true

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:20 28 4
gpt4 key购买 nike

我正在学习 Node Sequelize mysql 的教程,但我使用的是 postgresql。据我所知,sequelize 包会自动将 autoincrement true 转换为 postgres 方言中的串行数据类型,但对我来说,它会继续执行“id”INTEGER。

这就是我初始化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

这就是我初始化模型的方式

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

这是我运行服务器的 app.js

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))

这是我从终端得到的内容

[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;

我希望得到这个执行

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

因为每当我添加新记录时都会收到此错误

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

我已经安装了--save pg 和sequelize 包。这是我的 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"
}
}

最佳答案

您不需要将 id 添加为模型的一部分,sequelize 会自行帮助处理这部分。


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

保持这样,它会自行处理

关于node.js - Node Sequelize postgres 方言不断为类型 : Sequelize. INTEGER 创建 INTEGER 类型而不是 SERIAL 类型的 id 和 autoincrement: true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704339/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com