I'm using the Serverless Framework with the serverless-offline plugin. I've been developing an AWS Lambda function offline and so far haven't had many huge problems.
I need to do a more complicated SQL query, and so I opted to use the literal
method to write some pure SQL. I checked the log and saw that Sequelize (with sequelize-typescript) was assigning aliases to the table names so that they matched the model names (or in the case of table relationships, the aliases matched the key that the relationship was assigned to. So I wrote my SQL accordingly. I ended up with the following.
const customer = await this.findOne({
include: [Coupons, CustomersInfo],
where: {
email_address: {
[Op.eq]: sql.literal(`binary '${email}'`)
},
authorization_level: {
[Op.ne]: 6
},
[Op.and]: [
sql.literal(`
CASE WHEN '${coupon_code}' is null || '${coupon_code}' = ''
THEN (coupon.coupon_flag !=2 || coupon.coupon_flag is null)
ELSE Customers.referral = '${coupon_code}'
END
`)
]
},
});
So again, to clarify, in the logs I could see that the customers table was being aliased to "Customers" and the coupon table was being aliased to "coupon".
I did a bunch of local development offline using the serverless-offline
plugin, just put it up on lambda and... it doesn't work.
It doesn't work because for some reason on Lambda the same customers table is getting aliased as "l". If I edit my hard coded query to reference the customers table as "l", then it works fine on Lambda... but it stops working offline because offline it is getting aliased as "Customers".
Is there any way to force Sequelize to alias the table as a certain name? Or something I can do to normalize the names between the two environments?
question from:
https://stackoverflow.com/questions/65947908/using-the-serverless-framework-with-aws-sequelizes-auto-generated-table-aliase 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…