title: Node JS API Sequelize PostgreSQL UUID as primary key return error "column Nan does not exist"
body: <p>I'm working on a REST API for the backend of a simple e-commerce app using <strong>Node JS</strong>, <strong>PostgreSQL</strong> and <strong>Sequelize</strong>, and I'm facing an issue with Sequelize when I try to add a product to the shopping cart. It returns an error <code>&quot;column Nan does not exist&quot;</code> <br>
  Initially I was using Integer for the user Id as the primary key, then I changed for <strong>UUID</strong> to better suit the purpose.<br><br>
  The code I'm using for the models and migrations is the following:</p>
  <pre><code>//User model
  export default (sequelize, DataTypes) =&gt; {
    const User = sequelize.define(
      'User',
      {
        id: {
          type: DataTypes.UUID,
          defaultValue: DataTypes.UUIDV4,
          allowNull: false,
          primaryKey: true,
          unique: true,
        },
        name: {
          type: DataTypes.STRING,
          allowNull: false
        },
        password: {
          type: DataTypes.STRING,
          allowNull: false
        },
        email: {
          type: DataTypes.STRING,
          allowNull: false,
          unique: {
            name: 'user_email',
            msg: 'A user with this email already exists.'
          }
        },
      },
    User.associate = models =&gt; {
      User.hasOne(models.Cart, {
        foreignKey: 'userId',
        as: 'cart',
        onDelete: 'cascade'
      });
    };
  
    User.associate = models =&gt; {
      User.hasMany(models.Order, {
        foreignKey: 'userId',
        as: 'orders',
        onDelete: 'cascade'
      });
    };
  
    return User;
  };
  
  //User migration
  export const up = (queryInterface, Sequelize) =&gt;
    queryInterface.createTable('Users', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
        primaryKey: true,
        unique: true,
      },
      name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      password: Sequelize.STRING,
      email: {
        allowNull: false,
        type: Sequelize.STRING,
        unique: true
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('now')
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('now')
      },
    });
  
  export const down = queryInterface =&gt; queryInterface.dropTable('Users');
  </code></pre>
  <p>Cart model</p>
  <pre><code>export default (sequelize, DataTypes) =&gt; {
    const Cart = sequelize.define('Cart', {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        primaryKey: true,
      },
      userId: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false
      },
      cartItem: {
        type: DataTypes.TEXT,
        allowNull: false,
        get(value) {
          return JSON.parse(this.getDataValue(value));
        },
        set(value) {
          this.setDataValue('cartItem', JSON.stringify(value));
        }
      }
    });
  
    Cart.associate = models =&gt; {
      Cart.belongsTo(models.User, {
        foreignKey: 'userId',
        as: 'owner'
      });
    };
    return Cart;
  };
  </code></pre>
  <p>Cart migration</p>
  <pre><code>export const up = (queryInterface, Sequelize) =&gt;
    queryInterface.createTable('Carts', {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      userId: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false
      },
      cartItem: {
        type: Sequelize.TEXT,
        allowNull: false
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('now')
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('now')
      }
    });
  
  export const down = queryInterface =&gt; queryInterface.dropTable('Carts');
  </code></pre>
  <p>Code to handle the add to cart:</p>
  <pre><code>addToCart() {
      return this.asyncWrapper(async (req, res) =&gt; {
        const { body, user } = req;
  
        body.userId = user.id;
        const cart = await this.service.addToCart(body);
  
        this.sendResponse(res, cart, undefined, 201);
      });
    }
  </code></pre>
  <p>Add to cart service</p>
  <pre><code>async cart(userId, options = {}) {
      const cart = await super.find({ userId }, options);
      return cart;
    }
  
    async addToCart(data, options) {
      const { userId, productId, qty } = data;
  
      const [result] = await this.model.findOrCreate({
        where: { userId: +userId },
        defaults: { cartItem: new CartItem() }
      });
  
      const cartData = JSON.parse(result.dataValues.cartItem);
      const cartItem = new CartItem(cartData);
  
      const product = await ProductService.getById(productId, { plain: true });
  
      ExceptionHandler.throwErrorIfNull(product);
  
      const cart = cartItem.addToCart(product, qty);
  
      result.cartItem = cart;
      result.save();
  
      return result;
    }
  </code></pre>
  <p>The SQL query generated by Sequelize is the following:<br>
  <code>SELECT &quot;id&quot;,&quot;userId&quot;,&quot;cartItem&quot;,&quot;createdAt&quot;,&quot;updatedAt&quot; FROM &quot;Carts&quot; AS &quot;Cart&quot; WHERE &quot;Cart&quot;.&quot;userId&quot; = NaN LIMIT 1;</code></p>
  <p>The goal is to use <strong>UUID</strong> as primary key in the database.
  This issue started when I changed the Datatype from Integer for UUID and I can't see what is wrong with the code.</p>
  <p>Any advice on how to solve this?</p>
  <pre><code>Sequelize version: &quot;^5.21.9&quot; with &quot;pg&quot;: &quot;^8.2.0&quot; and &quot;pg-hstore&quot;: &quot;^2.3.3&quot;.
  </code></pre>