title: Sequelize model insert with array datatypes (postgresql)
body: <p>Hi can anyone help me how to create sequelize model that can be inserted with array datatypes. My scenario is that I have 2 models (<code>product</code> &amp; <code>order</code>), and what I want to do is that in order model I have <code>order_item</code> which will be array of <code>product_id</code> and refers to <code>product</code> table, so that I can get the rest of needed attributes.</p>
  <p>**Note: I planned to use with raw queries</p>
  <p>Here is my code:</p>
  <p><strong>Model Product</strong></p>
  <pre><code>const { DataTypes } = require(&quot;sequelize&quot;);
  const sequelize = require(&quot;../config&quot;);
  
  const Product = sequelize.define(
   &quot;products&quot;,
  {
      id: {
        primaryKey: true,
        type: DataTypes.UUID,
        allowNull: false,
  },
      p_image: {
      type: DataTypes.TEXT,
      allowNull: false,
  },
      p_name: {
      type: DataTypes.STRING,
      allowNull: false,
  },
      p_desc: {
      type: DataTypes.TEXT,
      allowNull: false,
  },
      p_prize: {
      type: DataTypes.INTEGER,
      allowNull: false,
  },
      p_size: {
      type: DataTypes.STRING,
      allowNull: false,
    },
   },
  {
    freezeTable: true,
   }
    );
    module.exports = Product;
  </code></pre>
  <p><strong>Model Order</strong></p>
  <pre><code>const { DataTypes, Deferrable } = require(&quot;sequelize&quot;);
  const sequelize = require(&quot;../config&quot;);
  const Product = require(&quot;./model_product&quot;);
  
  const Order = sequelize.define(&quot;orders&quot;, {
       id: {
          primaryKey: true,
          type: DataTypes.UUID,
          allowNull: false,
      },
      o_items: {
          type: DataTypes.ARRAY(DataTypes.UUID),
          allowNull: false,
          references: {
            model: Product,
            key: &quot;id&quot;,
            deferrable: Deferrable.INITIALLY_IMMEDIATE,
          },
      },
      o_quantity: {
          type: DataTypes.INTEGER,
          allowNull: false,
      },
      o_total_price: {
          type: DataTypes.INTEGER,
          allowNull: false,
      },
      });
  module.exports = Order;
  </code></pre>
  <p>I planned to make the insert query like below:</p>
  <pre><code>  const sql = `INSERT INTO orders (id, o_items, o_quantity, o_total_price, &quot;createdAt&quot;, &quot;updatedAt&quot;)
      VALUES (?, ?, ?, ?, now(), now())
      ON CONFLICT (id) DO NOTHING
      RETURNING *;`;
  </code></pre>
  <p>When I start server <code>npm start</code> I received <code>cannot be implemented</code>:</p>
  <pre><code>CREATE TABLE IF NOT EXISTS &quot;orders&quot; (&quot;id&quot; UUID NOT NULL , &quot;o_items&quot; UUID[] NOT NULL REFERENCES &quot;products&quot; (&quot;id&quot;) DEFERRABLE INITIALLY IMMEDIATE, &quot;o_quantity&quot; INTEGER NOT NULL, &quot;o_total_price&quot; INTEGER NOT NULL, &quot;createdAt&quot; TIMESTAMP WITH TIME ZONE NOT NULL, &quot;updatedAt&quot; TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY (&quot;id&quot;)); 
  foreign key constraint &quot;orders_o_items_fkey&quot; cannot be implemented
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>I checked and seemed its not possible to store array of foreign key, so I changed my model to <code>m:n</code> relations between <code>product</code> &amp; <code>order</code> through <code>order_items</code> like below:
  <a href="https://i.stack.imgur.com/Y8Im8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y8Im8.jpg" alt="enter image description here" /></a></p>
  <p>But still my question is how to insert array of products, let say ex: user in one order he can order 3 different products. Sorry if this question is stupid, but I'm new to this and anyone please help for this matter and also I wanted to do it with query because thats the requirement for the project. Thanks.</p>