我有这两个表:
const AdminUser = sequelize.define('AdminUser', {
adminUserId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
allowNull: false,
primaryKey: true
},
adminUsername: {
type: 'citext',
unique: true,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
role: {
type: DataTypes.ENUM,
values: ['super-admin', 'non-super-admin']
},
active: {
type: DataTypes.BOOLEAN
}
})
AdminUser.belongsToMany(MsTeam, {
through: 'AdminUserTeam',
foreignKey: {
name: 'adminUserId',
allowNull: false
}
})
和
const MsTeam = sequelize.define('MsTeam', {
teamId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
allowNull: false,
primaryKey: true
},
msTeamId: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
msTeamName: {
type: DataTypes.STRING,
allowNull: false
},
active: {
type: DataTypes.BOOLEAN
}
})
MsTeam.belongsToMany(models.AdminUser, {
through: 'AdminUserTeam',
foreignKey: {
name: 'teamId',
allowNull: false
}
})
他们通过'AdminUserTeam'建立了n:m关系。 时间戳已启用。
但是一个简单的查询,例如
// Include team data
const includeMsTeam = {
model: MsTeam,
through: {
attributes: []
},
attributes: ['teamId', 'msTeamName']
}
const users = await AdminUser.findAll({
attributes: ['adminUserId', 'adminUsername'],
limit: 10,
offset: 10,
order: [['createdAt', 'DESC']],
include: [includeMsTeam]
})
发生错误,
{
"error": "column AdminUser.createdAt does not exist"
}
奇怪的是,如果我删除了include
字段,查询就会成功
一个解决方案是禁用in this SO answer中提到的时间戳。但是,如果我想保留时间戳怎么办?如何使用关联通过“ createdAt”排序?
编辑:添加SQL查询
Executing (default): SELECT "AdminUser".*, "MsTeams"."teamId" AS "MsTeams.teamId", "MsTeams"."msTeamName" AS "MsTeams.msTeamName", "MsTeams->AdminUserTeam"."createdAt" AS "MsTeams.AdminUserTeam.createdAt", "MsTeams->AdminUserTeam"."updatedAt" AS "MsTeams.AdminUserTeam.updatedAt", "MsTeams->AdminUserTeam"."adminUserId" AS "MsTeams.AdminUserTeam.adminUserId", "MsTeams->AdminUserTeam"."teamId" AS "MsTeams.AdminUserTeam.teamId" FROM (SELECT "AdminUser"."adminUserId", "AdminUser"."adminUsername" FROM "AdminUsers" AS "AdminUser" WHERE "AdminUser"."role" = 'non-super-admin' AND ( SELECT "AdminUserTeam"."adminUserId" FROM "AdminUserTeam" AS "AdminUserTeam" INNER JOIN "MsTeams" AS "MsTeam" ON "AdminUserTeam"."teamId" = "MsTeam"."teamId" WHERE ("AdminUser"."adminUserId" = "AdminUserTeam"."adminUserId") LIMIT 1 ) IS NOT NULL ORDER BY "AdminUser"."createdAt" DESC LIMIT 20 OFFSET 0) AS "AdminUser" LEFT OUTER JOIN ( "AdminUserTeam" AS "MsTeams->AdminUserTeam" INNER JOIN "MsTeams" AS "MsTeams" ON "MsTeams"."teamId" = "MsTeams->AdminUserTeam"."teamId") ON "AdminUser"."adminUserId" = "MsTeams->AdminUserTeam"."adminUserId" ORDER BY "AdminUser"."createdAt" DESC;
错误发生在
ORDER BY "AdminUser"."createdAt" DESC
答案 0 :(得分:1)
似乎sequelize会生成许多子查询,并且"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var secondclass_model_1 = require("~/shared/secondclass.model");
var data_service_1 = require("~/shared/data.service");
var TestComponent = (function () {
function TestComponent(dataService) {
this.dataService = dataService;
// Use the component constructor to inject providers.
}
Object.defineProperty(TestComponent.prototype, "first", {
get: function () {
return this._first;
},
set: function (value) {
this._first = value;
},
enumerable: true,
configurable: true
});
;
Object.defineProperty(TestComponent.prototype, "second", {
get: function () {
return this._second;
},
set: function (value) {
this._second = value;
},
enumerable: true,
configurable: true
});
;
TestComponent.prototype.ngOnInit = function () {
};
TestComponent.prototype.itemTap = function (tickettype) {
var item = new secondclass_model_1.SecondClass({
foo: "bar"
});
this.dataService.addItem(item);
};
return TestComponent;
}());
__decorate([
core_1.Input("first"),
__metadata("design:type", Object),
__metadata("design:paramtypes", [typeof (_a = typeof firstclass_model_1.FirstClass !== "undefined" && firstclass_model_1.FirstClass) === "function" && _a || Object])
], TestComponent.prototype, "first", null);
__decorate([
core_1.Input("second"),
__metadata("design:type", Object),
__metadata("design:paramtypes", [typeof (_b = typeof secondclass_model_1.SecondClass !== "undefined" && secondclass_model_1.SecondClass) === "function" && _b || Object])
], TestComponent.prototype, "second", null);
TestComponent = __decorate([
core_1.Component({
selector: "test-component",
moduleId: module.id,
templateUrl: "./test.component.html",
styleUrls: ['./test.component.css']
}),
__metadata("design:paramtypes", [typeof (_c = typeof data_service_1.DataService !== "undefined" && data_service_1.DataService) === "function" && _c || Object])
], TestComponent);
exports.TestComponent = TestComponent;
var _a, _b, _c;
对最顶部的createdAt
不可见,请尝试将ORDER BY
添加到options对象。如果这样做没有帮助,可以将subQuery: false
添加到createdAt
中。
答案 1 :(得分:0)
你为什么不使用
sequelize.define('AdminUser', {
// Columns
}, {
timestamps: true,
});
并检查REAL DATABASE中是否有createdAt列,因为更改后继模型不会自动反映出对REAL DATABASE的更改。
答案 2 :(得分:0)
subQuery: false
无法正常工作..!
但是下面的代码将起作用:
var user = sequelize.define('user', { /* bla */ }, {
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false,
// your other configuration here
});