我已经创建了15个模型,并且只在几个表中随机获取每个列。是什么原因造成的呢?有人能帮我吗?
PS:我只包含了我认为与我的问题相关的代码部分
app.js:
// imports
const express = require('express');
const bodyParser = require('body-parser');
const app = express()
const port = 5000
//Body Parser
app.use(bodyParser.urlencoded({ extended: true }));
//Database connection
const db = require("./models");
db.sequelize.sync({ force: true }).then(() => {
console.log("Drop and re-sync db.");
});
//listen on port 5000
app.listen(port, () => console.info('Ready To Proceed'))
country.js(只获取默认列,如id, createdAt和updatedAt)
module.exports = (sequelize, Sequelize) => {
const Country = sequelize.define("country", {
name: {
type: Sequelize.STRING,
unique: true
},
flag: {
type: Sequelize.STRING
},
countryCode: {
type: Sequelize.STRING
}
});
return Country;
};
cart.js(获取我定义的所有列)
module.exports = (sequelize, Sequelize) => {
var User = sequelize.define('user');
var Product = sequelize.define('product');
const Cart = sequelize.define("cart", {
quantity: {
type: Sequelize.STRING
}
});
Cart.belongsTo(User);
Cart.belongsTo(Product);
return Cart;
};
index.js(同步所有模型)
const dbConfig = require("../config/db.config");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
port: dbConfig.PORT,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.country = require("./country.model")(sequelize, Sequelize);
db.cart = require("./cart.model")(sequelize, Sequelize);
module.exports = db;
db.config.js:
module.exports = {
HOST: "localhost",
USER: "<<USER>>",
PASSWORD: "<<Password>>",
DB: "redhuntdb",
dialect: "mysql",
PORT: 3307(My db port is 3307),
pool: {
max: 15,
min: 0,
acquire: 30000,
idle: 10000
}
};
如果有人遇到这个问题,并有同样的问题,我弄清楚我做错了什么。sequelize.define()
每次使用它都会创建一个新表。我在不知不觉中使用sequelize.define()
在另一个模型中创建一个模型的对象,这是不对的。