PostgreSQL 的 Sequelize 模式:如何在模型中准确定义模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42497254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize schema for PostgreSQL: How to accurately define a schema in a model?
提问by Val
I searched throughout the net and have not been able to determine how to add a schema to this sequelize model below. The following code does not kick back errors, however when I inspect the postgres DB, the only schema is the default one for public.
我在整个网络上进行了搜索,但无法确定如何向下面的此续集模型添加模式。以下代码不会回退错误,但是当我检查 postgres 数据库时,唯一的架构是 public 的默认架构。
// The model definition is done in /path/to/models/project.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define("project", {
name: DataTypes.STRING,
description: DataTypes.TEXT,
},
define: {
schema: "prefix"
},
classMethods: {
method1: function() {},
method2: function() {}
},
instanceMethods: {
method3: function() {}
})
How should the script be revised to accurately define a schema?
应如何修改脚本以准确定义模式?
EDIT
编辑
In my case, the final answer was
就我而言,最终答案是
database_name.sequelize.createSchema('prefix').then(() => {...});
in my ./models/index.js file the database object is as follows:
在我的 ./models/index.js 文件中,数据库对象如下:
database_name = {
Sequelize: Sequelize,
sequelize: sq,
table_1: sq.import(__dirname + '/file_folder')
};
module.exports = database_name;
回答by piotrbienias
Your model definition should look as follows
您的模型定义应如下所示
module.exports = function(sequelize, DataTypes) {
return sequelize.define("project", {
name: DataTypes.STRING,
description: DataTypes.TEXT,
}, {
schema: 'prefix',
classMethods: {
method1: function() {},
method2: function() {}
},
instanceMethods: {
method3: function() {}
}
}
}
According to the documentation of options
object in sequelize.define
method, it can have attribute called schema
.
根据方法中options
对象的文档sequelize.define
,它可以具有名为schema
.
EDIT- Creating schema programatically
编辑- 以编程方式创建架构
In order to create a new schema (only for PostgreSQL!), you can use the sequelize.createSchema()
method:
为了创建新模式(仅适用于 PostgreSQL!),您可以使用以下sequelize.createSchema()
方法:
sequelize.createSchema('prefix').then(() => {
// new schema is created
});
Above creates given SQL
以上创建给定的 SQL
CREATE SCHEMA prefix;
In order to use this schema in model definitions, you need to create the schema before synchronising any model into the database - it could be run before sequelize.sync()
or, if you use migrations, as a first migration file.
为了在模型定义中使用此架构,您需要在将任何模型同步到数据库之前创建该架构 - 它可以在之前运行,sequelize.sync()
或者如果您使用迁移,则作为第一个迁移文件运行。
回答by Edward Smith
I think you need to define the schema in the create table migration file like so:
我认为您需要在创建表迁移文件中定义架构,如下所示:
queryInterface.createTable(
'nameOfTheNewTable',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
createdAt: {
type: Sequelize.DATE
},
updatedAt: {
type: Sequelize.DATE
},
attr1: Sequelize.STRING,
attr2: Sequelize.INTEGER,
attr3: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false
},
//foreign key usage
attr4: {
type: Sequelize.INTEGER,
references: {
model: 'another_table_name',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
}
},
{
engine: 'MYISAM', // default: 'InnoDB'
charset: 'latin1', // default: null
schema: 'prefix' // default: public, PostgreSQL only.
}
回答by krys Funtain
This code works with "sequelize": "^4.23.2","pg": "^7.4.0", "pg-hstore": "^2.3.2",
此代码适用于 "sequelize": "^4.23.2","pg": "^7.4.0", "pg-hstore": "^2.3.2",
const User = sequelize.define('people', {
uuid: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV1,
primaryKey: true
},
username: Sequelize.STRING,
email: Sequelize.STRING,
birthday: Sequelize.DATE
}, {
schema: 'public',
});
sequelize.sync()
.then(() => User.create({
username: 'MartialDoane',
email: '[email protected]',
birthday: new Date(1977, 6, 11)
}))
.then(jane => {
console.log(jane.toJSON());
res.send(jane);
res.status(200);
});
That will create the table in schema public and not my default schema.
这将在模式 public 中创建表,而不是我的默认模式。