如何在数据库中存储一组颜色?

How to store an array of colors in a database?

I need create this functionality,我正在创建 nike.com 的克隆,但我遇到了问题。如何创建具有颜色选项的产品 table?我应该如何将这些数据存储在数据库中(PostgreSQL + Sequelize)? 我已经创建了两个 tables:Product 和 Color,现在我想 link 它们,所以在创建产品 table 时,我可以从 Color [=16] 添加现有颜色=].

  async create(req: Request & { files: any }, res: Response, next: NextFunction) {
    try {
      const { name, price, typeId, categoryId } = req.body
      const colors = [1, 2, 3]
      const { img } = req.files
      let fileName = uuidv4() + '.jpg' 
      img.mv(path.resolve(__dirname, '..', 'static', fileName))
      const product = await Product.create({
        name,
        price,
        img: fileName,
        typeId,
        categoryId,
        colors: //[2, 1] how to select from colorTable?
      })
      return res.json(product)
    } catch (e) {
      next(ApiError.BadRequest(e.message))
    }
  }

const Product = sequelize.define('product', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  img: { type: DataTypes.STRING, allowNull: false },
  name: { type: DataTypes.STRING, unique: true, allowNull: false },
  price: { type: DataTypes.INTEGER, allowNull: false },
  rating: { type: DataTypes.INTEGER, defaultValue: 0 },
})

const Color = sequelize.define('color', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  color: { type: DataTypes.STRING, unique: true, allowNull: false },
})


Color.belongsToMany(Product, { through: TypeColor })
Product.belongsToMany(Color, { through: TypeColor })

我不确定你在找什么。 如果您想 select 在颜色 table 中着色,为什么不在创建产品之前 select?

因为你使用异步,我认为在 table 中使用异步调用颜色(你想要的颜色数据)没有问题!

喜欢下面!!

async create(req: Request & { files: any }, res: Response, next: NextFunction) {
    try {
      const { name, price, typeId, categoryId } = req.body
      const colors = [1, 2, 3]
      const { img } = req.files
      let fileName = uuidv4() + '.jpg' 
      img.mv(path.resolve(__dirname, '..', 'static', fileName))

     const color = await Color.findAll({
      where :{} // here goes with your conditions      
})
      const product = await Product.create({
        name,
        price,
        img: fileName,
        typeId,
        categoryId,
        colors: //[2, 1] how to select from colorTable?
      })
      return res.json(product)
    } catch (e) {
      next(ApiError.BadRequest(e.message))
    }
  }

你描述的是多对多(M:M)是数据库术语。一种产品有多种颜色,一种颜色被多种产品使用。这通过创建第三个 table (Product_Color) 来解决。此 table 包含每个基 table 的 ID 作为其自己的 PK,每个作为适当 table 的 FK。我不知道 Sequelize,但在 Postgres 中,table 定义类似。

create table products( 
             prod_id integer generated always as identity 
           , name    test 
           -- additional product atteibutes 
           , constraint products_pk
                        primary key(prod_id)           
           ); 

create table colors(
             color_id integer generated always as identity 
           , name     text 
           -- additional color attributs
           , constraint colors_pk
                        primary key(color_id)  
           ); 

          
-- Resolution table for M:M products:colors
create table product_colors(
             prod_id  integer 
           , color_id integer
           -- intersection attributes 
           , constraint products_colors_pk
                        primary key (prod_id,color_id)
           , constraint products_colors_2_products_fk
                        foreign key (prod_id) 
                        references products(prod_id)
           , constraint products_colors_2_colors_fk
                        foreign key (color_id) 
                        references colors(color_id)
           );

不要存储为数组。它们最初很简单,但尝试用它们做任何事情很快就会变得复杂。就像按产品列出颜色一样。它违反了 1NF 形式,参见 here and here