我似乎无法找到将列从字符串类型更改为 ENUM 的正确方法,同时保留该列中的数据。
我还尝试创建一个具有 ENUM 类型的新列,然后在列之间复制数据:
// migrations/20160606170538-change-column.js
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.addColumn('time', 'newcolumn', {
allowNull: true,
type: Sequelize.ENUM('1-day', '7-day', '1-month', '3-month', '6-month', '1-year')
}).then(function () {
return queryInterface.sequelize.query("UPDATE time SET newcolum = oldcolumn");
});
},
down: function (queryInterface, Sequelize) {
}
};
Run Code Online (Sandbox Code Playgroud)
但我在迁移时返回以下错误:
错误:列“newcolumn”的类型为 enum_time_newcolumn 但表达式的类型为字符变化]
这个简单的测试代码:
return queryInterface.createTable('tadam', {id: Sequelize.INTEGER, humus: Sequelize.STRING(255)})
.then(queryInterface.sequelize.query('ALTER TABLE tadam ADD PRIMARY KEY (id)'));
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
Unhandled rejection SequelizeDatabaseError: relation "tadam" does not exist
Run Code Online (Sandbox Code Playgroud)
现在,据我所知,当第二个承诺(关于改变表格)被执行时,该表尚未创建.
这可能不是因为迁移中的所有查询都是一次执行的,因为我有这个测试迁移:
return queryInterface.sequelize.query('ALTER TABLE tadam DROP CONSTRAINT tadam_pkey')
.then(queryInterface.removeIndex('tadam', 'tadam_pkey'));
Run Code Online (Sandbox Code Playgroud)
它工作正常.
那么,任何人都可以解释为什么第一个不起作用以及如何实现它,以便创建表+添加PK可以从单个迁移执行?
我一直在阅读大量的sequelize-cli文档,似乎无法弄清楚如何将列添加到我现有的模型中.我有一个名为的模型,models/team.js并希望添加一个名为role_name模型的列sequelize model:create --name team --attributes role_name:string,但是我收到一条消息要覆盖而不是修改:
Loaded configuration file "config/config.json".
Using environment "development".
The file /Users/user/Desktop/Projects/node/app-repo/app/models/team.js already exists. Run "sequelize model:create --force" to overwrite it.
Run Code Online (Sandbox Code Playgroud)
我不想覆盖使我认为这不是正确使用命令的文件.它还让我想知道cli是否无法实现这一点,必须在迁移文件级别进行调整.
我将Node Express与Sequelize一起使用,并且要将数据库中“日期”列的defaultValue设置为今天的日期。我尝试了以下迁移,但没有成功,因为它将默认日期设置为与运行迁移相同的日期。我希望将其设置为与创建行时相同的日期。
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.addColumn(
'Todos',
'date',
{
type: Sequelize.DATEONLY,
allowNull: false,
defaultValue: new Date()
}
)
},
Run Code Online (Sandbox Code Playgroud)
我不明白那将如何工作。
我使用 Sequelize 连接到我的 PostgreSQL 数据库,在开发过程中,我使用种子文件用示例数据填充数据库。我最近为我的数据库安装了 PostGIS,并想使用 GEOMETRY('POINT') 类型来描述纬度/经度位置。
但是,我不知道如何使用播种机放置一些 GEOMETRY 数据。我尝试按照Sequelize 文档中的示例进行操作:
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.bulkInsert('Vets', [{
title: 'Centrum Zdrowia Ma?ych Zwierz?t',
city: 'Pozna?',
googleMapsID: 'ChIJQ8EgpGpDBEcR1d0wYZTGPbI',
position: {
type: 'Point',
coordinates: [52.458415, 16.904740]
},
rodents: true
}], {}),
down: (queryInterface, Sequelize) =>
queryInterface.bulkDelete('Vets', null, {})
};
Run Code Online (Sandbox Code Playgroud)
但是当我运行sequelize db:seed:all命令时,出现以下错误:
ERROR: Invalid value [object Object]
我想我只需要以其他方式指定位置,但 Sequelize 文档没有提到任何种子。谁能帮我解决这个问题?
Vets数据库的迁移文件如下:
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Vets', {
...
rodents: {
type: …Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来按顺序复制/克隆实例,但没有成功。有没有内置函数可以做到这一点?我想要的是简单地复制数据库中的行,而新项应仅具有不同的ID。
我使用 sequelize-cli 创建了一个表格,但是我忘记添加一列:标题。
所以我生成了新的迁移:
$ sequelize migration:create --name update-notes
Run Code Online (Sandbox Code Playgroud)
并将这些代码放在迁移文件中:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn(
'Notes',
'title',
Sequelize.STRING
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn(
'Notes',
'title'
);
}
};
Run Code Online (Sandbox Code Playgroud)
运行迁移并从数据库检查表架构后,它可以工作:
$ sequelize db:migrate
Run Code Online (Sandbox Code Playgroud)
但是模型还没有我新添加的列:
'use strict';
// models/notes.js
module.exports = (sequelize, DataTypes) => {
const Notes = sequelize.define('Notes', {
content: DataTypes.TEXT // NO 'TITLE' COLUMN
}, {});
Notes.associate = function(models) {
// associations can be defined here
};
return Notes; …Run Code Online (Sandbox Code Playgroud) updatedAt我的表中有一个User类型的列Date,它存储日期和时间。我想仅使用日期而不是日期时间进行查询。我正在使用sequelizeSequelize.Op.gt和Sequelize.Op.lt运算Users符通过添加24 * 60 * 60 * 1000. 但日期不会增加一天。当我尝试减法时,它工作得完美无缺。我只能在使用 getTime() 方法后添加一天。
我很困惑为什么它在不使用减法时有效getTime()但在加法时不起作用。有人能解释一下吗?
长话短说
这有效:
[Sequelize.Op.gt]: new Date(new Date(updatedAt) - 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27
//output: 2018-11-26
Run Code Online (Sandbox Code Playgroud)
这不起作用:
[Sequelize.Op.gt]: new Date(new Date(updatedAt) + 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27
//output: 2018-11-27
Run Code Online (Sandbox Code Playgroud)
这有效:
[Sequelize.Op.lt]: new Date(new Date(updatedAt).getTime() + 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27 …Run Code Online (Sandbox Code Playgroud) 我想在我的express api上使用sequelize播种机和迁移,目前所有模型都是使用sequelize-typescript以typescript编写的
我尝试使用打字稿添加我的第一个种子文件,但运行时出现错误
20221028050116-feeds.ts播种文件
'use strict';
import { QueryInterface } from 'sequelize';
const feedTypes = [
{ id: 'b871a455-fddb-414c-ac02-2cdee07fa671', name: 'crypto' },
{ id: '68b15f90-19ca-4971-a2c6-67e66dc88f77', name: 'general' },
];
const feeds = [
{
id: 1,
name: 'cointelegraph',
url: 'https://cointelegraph.com/rss',
feed_type_id: 'b871a455-fddb-414c-ac02-2cdee07fa671',
},
];
module.exports = {
up: (queryInterface: QueryInterface): Promise<number | object> =>
queryInterface.sequelize.transaction(async (transaction) => {
// here go all migration changes
return Promise.all([
queryInterface.bulkInsert('feed_types', feedTypes, { transaction }),
queryInterface.bulkInsert('feeds', feeds, { transaction }),
]);
}), …Run Code Online (Sandbox Code Playgroud) database-migration node.js sequelize.js typescript sequelize-cli
我有两个模型:
user.js
'use strict'
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
gid: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
newsletters: {
type: 'NUMERIC',
allowNull: false,
defaultValue: '1'
},
status: {
type: 'NUMERIC',
allowNull: false,
defaultValue: '1'
},
date_verified: {
type: DataTypes.TIME,
allowNull: true
},
date_created: {
type: DataTypes.TIME,
allowNull: false,
defaultValue: sequelize.fn('now')
},
date_updated: {
type: DataTypes.TIME,
allowNull: false,
defaultValue: …Run Code Online (Sandbox Code Playgroud)sequelize-cli ×10
sequelize.js ×10
node.js ×7
postgresql ×3
enums ×1
express ×1
graphql ×1
javascript ×1
orm ×1
postgis ×1
typescript ×1
umzug ×1