我再次感到困惑。
在使用Sequelize定义数据库(MySQL)表时,我想 sync(force:true),以删除/创建表。到目前为止,一切都很好。
现在,我对我的user
表感到满意,并且不想在每次运行时都将其删除,而我继续定义其他表。
因此,我想在user
选项sync: {forced: false}
module.exports = (sequelize, DataTypes) => {
let Schema = sequelize.define(
"user",
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
BlaBla:{}
},
{ // options
sync: {force: false},
freezeTableName: true,
timestamps: true
});
return Schema;
};
这不起作用。每次运行都会删除/创建表。
继续定义我的company
表。
module.exports = (sequelize, DataTypes) => {
let Schema = sequelize.define(
"company",
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
BlaBla:{}
},
{ // options
sync: {force: true},
freezeTableName: true,
timestamps: false
});
return Schema;
};
问题
sequelize.define
选项有选择地同步表?答案 0 :(得分:1)
不支持在sequelize.define()
中选择性同步。有关详细信息,请参见Model Definition Configuration。在项目的其他地方,必须有一个sequelize.sync({force: true})
的调用,该调用正在同步所有模型。您应该先更正此问题,因为这会强制所有模型同步。
解决此问题后,您可以进行一些选择:
1:继续使用迁移。对架构的每次添加或更改都会在迁移中捕获,因此您可以推出每个更改而无需重新同步其他更改。 参见http://docs.sequelizejs.com/manual/migrations.html
2:根据您的建议分别同步每个表。您需要分别为每个模型调用sync()
方法,例如:
const user = require('./user');
const company = require('./company');
company.sync();
compant.sync({force:true});
这对于早期开发可能是可行的,但是随着您在项目中进行进展并开始发布,迁移应该是推出架构更改的选择方法。
请参见http://docs.sequelizejs.com/manual/models-definition.html#database-synchronization
答案 1 :(得分:0)
好吧,经过一整夜的睡眠,我的大脑又开始工作了。
db object
db[model.name] = model
db.Sequelize = Sequelize
的引用db.sequelize = sequelize
的引用仅需要实例const sequelize = new Sequelize()
时。
给出OP中的model definition
。
文件:app/models/user.js
module.exports = (sequelize, DataTypes) => {
let Schema = sequelize.define(
"user",
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
BlaBla:{}
},
{ // options
sync: {force: false},
freezeTableName: true,
timestamps: true
});
return Schema;
};
还有这样的引导程序:
文件:config/sequelize.js
const Sequelize = require("sequelize");
const sequelize = new Sequelize(config.dbname, config.dbuser, config.dbpass, {
dialect: "mysql",
host: config.dbhost,
port: 3306,
define: {
engine: 'MYISAM',
timestamps: true,
}
});
fs
.readdirSync(path.join(config.models))
.filter(file => ~file.indexOf('.js'))
.forEach((file) => {
let model = sequelize["import"](path.join(config.models, file));
model.sync(model.options.sync);
});
module.exports = sequelize;
然后您将每个模型同步一次。
然后在server.js
中输入
const sequelize = require(join(__dirname,"config/sequelize"));
/* debug */
Object.keys(sequelize.models).forEach(function(name) {
console.log('sequelize model:',name);
});
console.log('Checking DB connection...');
sequelize
.authenticate()
.then(() => {
console.log('Successfully connected to:', config.dbname);
})
.catch(err => {
console.error('Unable to connect to the database:', err);
process.exit;
});
const User = sequelize.model('user');
这导致user
不被删除,但company
IS 被删除。
这是运行的控制台输出:
> node server.js
config/index.js { dbhost: '192.168.1.70',
dbport: 3306,
dbname: 'dbdev',
dbuser: 'dbdev',
dbpass: '********',
mode: 'development',
root: 'C:\\Workspace\\Github\\******',
models: 'C:\\Workspace\\Github\\******\\app\\models',
sync: false }
Creating DB connection...
Loading Schema: company
company sync { force: true }
Loading Schema: user
user sync { force: false }
sequelize model: company
sequelize model: user
Checking DB connection...
Server fetching: user
Executing (default): SELECT 1+1 AS result
Executing (default): DROP TABLE IF EXISTS `company`;
Executing (default): CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER UNSIGNED auto_increment , `login` VARCHAR(45) UNIQUE, `password` VARCHAR(255), `fname` VARCHAR(45), `lname` VARCHAR(45), `email` VARCHAR(128), `phone` VARCHAR(20), `company` INTEGER UNSIGNED, `createdby` INTEGER UNSIGNED, `access` INTEGER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=MYISAM;
Successfully connected to: dbdev
Executing (default): SHOW INDEX FROM `user`
Executing (default): CREATE TABLE IF NOT EXISTS `company` (`id` INTEGER auto_increment , `name` VARCHAR(255), `type` INTEGER, `createdby` INTEGER, `access` INTEGER, PRIMARY KEY (`id`)) ENGINE=MYISAM;
Executing (default): SHOW INDEX FROM `company`
我可能会看一下migrations
,但就目前而言,我希望根据需要在定义文件中进行初始建模。
我是否错过了一些要点,或者此解决方案“飞起来”了?