Sequalize
创建了3个SQL表,模式如下所示:Loans
DataTypes.INTEGER
,DataTypes.INTEGER
,DataTypes.INTEGER
,DataTypes.DATE
,DataTypes.DATE
,DataTypes.DATE
Books
DataTypes.INTEGER
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.INTEGER
Patrons
DataTypes.INTEGER
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.STRING
,DataTypes.INTEGER
Loans
查询Sequalize
表,SQLite
生成的Sequalize
查询应如下所示:SELECT
book.title AS book,
patron.first_name||' '||patron.last_name AS patron,
loaned_on,
return_by,
returned_on
FROM loans
JOIN books ON loans.book_id=books.id
JOIN patrons ON loans.patron_id=patrons.id
// Datebase relations
books.hasMany(loans, {
foreignKey: 'book_id'
});
loans.belongsTo(books, {
foreignKey: 'book_id'
});
patrons.hasMany(loans, {
foreignKey: 'patron_id'
});
loans.belongsTo(patrons, {
foreignKey: 'patron_id'
});
// All Loans page
router.get('/all_loans', function(req, res, next) {
loans.findAll({
attributes: [
[Sequelize.col('book.title'), 'book'],
[sequelize.literal("patron.first_name||' '||patron.last_name"), 'patron'],
'loaned_on',
'return_by',
'returned_on'
],
include: [books, patrons]
}).then();
res.render('all_loans', {
title: "All Loans"
});
});
SELECT
`loan`.`id`,
`book`.`title` AS `book`,
patron.first_name||' '||patron.last_name AS `patron`,
`loan`.`loaned_on`,
`loan`.`return_by`,
`loan`.`returned_on`,
`book`.`id` AS `book.id`,
`book`.`title` AS `book.title`,
`book`.`author` AS `book.author`,
`book`.`genre` AS `book.genre`,
`book`.`first_published` AS `book.first_published`,
`book`.`createdAt` AS `book.createdAt`,
`book`.`updatedAt` AS `book.updatedAt`,
`patron`.`id` AS `patron.id`,
`patron`.`first_name` AS `patron.first_name`,
`patron`.`last_name` AS `patron.last_name`,
`patron`.`address` AS `patron.address`,
`patron`.`email` AS `patron.email`,
`patron`.`library_id` AS `patron.library_id`,
`patron`.`zip_code` AS `patron.zip_code`,
`patron`.`createdAt` AS `patron.createdAt`,
`patron`.`updatedAt` AS `patron.updatedAt`
FROM `loans` AS `loan`
LEFT OUTER JOIN `books` AS `book` ON `loan`.`book_id` = `book`.`id`
LEFT OUTER JOIN `patrons` AS `patron` ON `loan`.`patron_id` = `patron`.`id`;
该查询还会从books
和patrons
表中提取所有列,我不想指定这些列。
答案 0 :(得分:0)
经过一些研究,我想出了如何修复我的Sequalize
代码:
loans.findAll({
attributes: [
[Sequelize.col('book.title'), 'book_title'],
[sequelize.literal("patron.first_name||' '||patron.last_name"), 'patron_name'],
'loaned_on',
'return_by',
'returned_on'
],
include: [{
model: books,
attributes: []
}, {
model: patrons,
attributes: []
}]
}).then(results => {
console.dir(results);
});
它成功生成以下SQLite代码:
SELECT
`loan`.`id`,
`book`.`title` AS `book_title`,
patron.first_name||' '||patron.last_name AS `patron_name`,
`loan`.`loaned_on`,
`loan`.`return_by`,
`loan`.`returned_on`
FROM `loans` AS `loan`
LEFT OUTER JOIN `books` AS `book` ON `loan`.`book_id` = `book`.`id`
LEFT OUTER JOIN `patrons` AS `patron` ON `loan`.`patron_id` = `patron`.`id`;
我似乎无法将book.title
替换为book
或patron.first_name||' '||patron.last_name
为patron
,因为稍后会在查询中使用这些别名。