我在写这篇文章的时候看到了很多问题。没有一个问题有同样的问题,在连续20个小时试图解决这个问题之后,我感到很有压力,所以如果我没有看到类似的问题,我很抱歉。好的,问题如下:我用的是nodej,expressj(4.17),后果-临床试验(6.2.0)及后续(6.3.5)做一个服务器并与mysql通讯(mysql 2 2.1.0),并且由于某种原因,当我使用函数sequelize.sync()
时(不管有没有{force:true}
)它只是不更新数据库中已经创建的表,它什么也不做,没有错误,没有警告,我只是没有做应该做的事情。我使用npx sequelize model:generate --name ... --attributes ...
创建模型,这样创建的迁移就不会有我手动插入的任何内容,因为我搜索了不应该更改迁移文件的内容。然后我运行npx sequelize db:migrate
来升级到数据库的迁移,最后对没有同步的模型做了一些更改。
以下是server.js代码:
const http = require('http')
const Sequelize = require('sequelize')
const app = require('./src/app')
const port = process.env.PORT || 3000
const server = http.createServer(app)
const contatos = require('./src/_db/models/contatos')
const corretores = require('./src/_db/models/corretores')
const imoveis = require('./src/_db/models/imoveis')
const tipoimovel = require('./src/_db/models/tipoimovel')
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: 'localhost',
dialect: "mysql" /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
logging: console.log,
})
sequelize.authenticate().then( () => {
console.log('Connection has been established successfully.');
sequelize.sync({logging:console.log, force:true})
.then(() => {
// console.log(`Database & tables created!`);
return server.listen(port)
}).catch((error)=>{
return console.error(error);
});
}).catch((error) => {
console.error('Unable to connect to the database:', error);
})
我的app.js代码:
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const Sequelize = require('sequelize')
const app = express()
app.use(morgan('dev'))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Accept, Authorization'
)
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET')
return res.status(200).json({})
}
next()
})
app.use((req, res, next) => {
const error = new Error('Not found')
error.status = 404
next(error)
})
app.use((error, req, res, next) => {
res.status(error.status || 500)
res.json({
error: {
message: error.message
}
})
})
module.exports = app
全部的控制台结果:
[nodemon] restarting due to changes...
[nodemon] starting 'node server.js'
Executing (default): SELECT 1+1 AS result
Connection has been established successfully.
Executing (default): SELECT 1+1 AS result
Database & tables created!
最后是我尝试用来更新数据库的Model示例,它已经包含了我所做的更改:
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class tipoImovel extends Model {
static associate(models) {
// define association here
this.hasOne(models.Imoveis)
}
};
tipoImovel.init({
tipoNome: {type:DataTypes.STRING,allowNull:false},
tipoSigla: {type: DataTypes.STRING(2),allowNull:false,unique:true}
}, {
sequelize,
modelName: 'tipoImovel',
});
return tipoImovel;
};
1条答案
按热度按时间nfeuvbwi1#
我用过这样的同步,它对我很有效。如果你想的话,你可以试试它。