So, I want to make relationships for these 5 tables and make findAll and get information from each table. Sorry for the ugly table display
Product table
| Field | Type | Null | Key | Default | Extra | | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | typeId | int(11) unsigned | NO | MUL | NULL | | | image | varchar(255) | YES | | NULL | | | desc | text | YES | | NULL | | | price | float | YES | | NULL | | | stock | int(11) | YES | | NULL | |
Type table
| Field | Type | Null | Key | Default | Extra | | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | |
Specification table
| Field | Type | Null | Key | Default | Extra | | productId | int(11) unsigned | NO | PRI | NULL | | | name | text | YES | | NULL | |
JctProductColors Table
| Field | Type | Null | Key | Default | Extra | | productId | int(11) unsigned | NO | PRI | NULL | | | colorId | int(11) unsigned | NO | PRI | NULL | |
Color chart
| Field | Type | Null | Key | Default | Extra | | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | |
This is the relationship that I have right now
Product.belongsTo(Spec, { "foreignKey": "id", "through": { model: "ProductSpec", unique: false }, "constraints": false }); Spec.belongsTo(Product, { "foreignKey": "productId", "through": { model: "ProductSpec", unique: false }, "constraints": false }); Type.belongsToMany(Product, { "constraints": false, "foreignKey": "id", "through": { model: "ProductType", unique: false } }); Product.belongsTo(Type, { "constraints": false, "foreignKey": "typeId", "through": { model: jctProductColor, unique: false } }); Product.belongsToMany(Color, { "constraints": false, "foreignKey": "productId", "through": { model: jctProductColor, unique: false } }); Color.belongsToMany(Product, { "constraints": false, "foreignKey": "colorId", "through": { model: jctProductColor, unique: false } });
I want to make findAll to display this
select types.name as Type, products.image, products.desc, products.price, products.stock, specs.name as Specs, colors.name as Color from products join types on types.id = products.typeId join specs on products.id = specs.productId join jctproductcolors on jctproductcolors.productId = products.id join colors on colors.id = jctproductcolors.colorid where products.id = :id
source share