express - sequelize freezeTableName but still plural table name in sql query -
i'm using express+ionic+mysql+sequelize develop app, got error in sequelize.
got 3 tables
xx_product
xx_product_specification_value
xx_specification_value
the relations between them below:
xx_product hasmany xx_product_specification_value xx_specification_value hasmany xx_product_specification_value xx_product_specification_value belongsto xx_product xx_product_specification_value belongsto xx_specification_value
here code:
models/products.js
module.exports = function(sequelize, datatypes){ return xx_product = sequelize.define('xx_product', { price: { type: datatypes.decimal }, full_name: { type: datatypes.string }, name: { type: datatypes.string }, image: { type: datatypes.string }, introduction: { type: datatypes.text }, xxsupplierid: { type: datatypes.bigint, field: 'supplier' }, xxbrandid: { type: datatypes.bigint, field: 'brand' } },{ freezetablename: true, tablename: 'xx_product', timestamps: false, classmethods: { associate: function(models) { xx_product.belongsto(models.xx_supplier, { foreignkey: 'supplier' }) xx_product.belongsto(models.xx_brand, { foreignkey: 'brand' }) xx_product.hasmany(models.xx_product_specification_value) } } }) }
models/product_specification_value.js
module.exports = function(sequelize, datatypes){ return xx_product_specification_value = sequelize.define('xx_product_specification_value', { xxproductid: { type: datatypes.bigint, field: 'products' }, xxspecificationvalueid: { type: datatypes.bigint, field: 'specification_values' } },{ freezetablename: true, tablename: 'xx_product_specification_value', timestamps: false, classmethods: { associate: function(models) { xx_product_specification_value.belongsto(models.xx_product, { foreignkey: 'products' }) xx_product_specification_value.belongsto(models.xx_specification_value, { foreignkey: 'specification_values', }) } } }) }
models/specification_value.js
module.exports = function(sequelize, datatypes){ return xx_specification_value = sequelize.define('xx_specification_value', { name: { type: datatypes.string }, image: { type: datatypes.string }, xxspecificationid: { type: datatypes.bigint, field: 'specification' } },{ freezetablename: true, tablename: 'xx_specification_value', timestamps: false, classmethods: { associate: function(models) { xx_specification_value.hasmany(models.xx_product_specification_value) xx_specification_value.belongsto(models.xx_specification, { foreignkey: 'specification' }) } } }) }
routes/product.js
var models = require('../models') exports.show = function(req, res){ var id = req.params.id models.xx_product.findone({ where: { id: id, }, include: [ { model: models.xx_product_specification_value, include: [ { model: models.xx_specification_value, include: [ { model: models.xx_specification } ] } ] } ] }) .then(function(product){ res.json(product) }) }
when invoke show in routes/products.js query, got error in console below:
unhandled rejection sequelizedatabaseerror: er_bad_field_error: unknown column 'xx_product_specification_values.id' in 'field list'
the sql query in console below:
executing (default): select `xx_product`.`id`, `xx_product`.`price`, `xx_product`.`full_name`, `xx_product`.`name`, `xx_product`.`image`, `xx_product`.`introduction`, `xx_product`.`supplier` `xxsupplierid`, `xx_product`.`brand` `xxbrandid`, `xx_product`.`supplier`, `xx_product`.`brand`, `xx_product_specification_values`.`id` `xx_product_specification_values.id`, `xx_product_specification_values`.`products` `xx_product_specification_values.xxproductid`, `xx_product_specification_values`.`specification_values` `xx_product_specification_values.xxspecificationvalueid`, `xx_product_specification_values`.`products` `xx_product_specification_values.products`, `xx_product_specification_values`.`specification_values` `xx_product_specification_values.specification_values` `xx_product` `xx_product` left outer join `xx_product_specification_value` `xx_product_specification_values` on `xx_product`.`id` = `xx_product_specification_values`.`products` `xx_product`.`id` = '1742';
my question why query xx_product_specification_values not xx_product_specification_value, why it's plural? have set freeszetablename = true , set tablename = xx_product_specification_value
i make big mistake relation define between tables, see, relation between xx_product , xx_specification_value should many_to_many,so in sequelize should define them below:
models/product.js
xx_product.belongstomany(models.xx_specification_value, { through: { model: models.xx_product_specification_value, unique: false }, foreignkey: "products" })
models/specification_value.js
xx_specification_value.belongstomany(models.xx_product, { through: { model: models.xx_product_specification_value, unique: false }, foreignkey: "specification_values" })
Comments
Post a Comment