Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - SequelizeDatabaseError with join table (Code: 42P01)

I am trying to create a join table for many to many relationships.

I am getting the following error:

"name": "SequelizeDatabaseError",
"parent": {
    "name": "error",
    "length": 110,
    "severity": "ERROR",
    "code": "42P01",
    "position": "13",
    "file": "parse_relation.c",
    "line": "1160",
    "routine": "parserOpenTable",
    "sql": "INSERT INTO \"user_routes\" (\"id\",\"userId\",\"routeId\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,'1','1','2017-11-15 03:57:21.791 +00:00','2017-11-15 03:57:21.791 +00:00') RETURNING *;"
},

The relationship is between User and Route:

module.exports = (sequelize, DataTypes) => {
  const Route = sequelize.define("Route", {
    open: {
      type: DataTypes.BOOLEAN,
      allowNull: true
    }
  });

  Route.associate = models => {
    Route.belongsToMany(models.User, {
      through: "userRoutes",
      as: "users"
    });
  };
  return Route;
};


module.exports = (sequelize, DataTypes) => {
  var User = sequelize.define("User", {
    email: DataTypes.TEXT,
    password: DataTypes.TEXT
  });

  User.associate = models => {
    User.belongsToMany(models.Route, {
      through: "userRoutes",
      as: "routes"
    });
  };

  return User;
};

Migration files for user and route does not have much but just the basics. For join table:

"use strict";
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable("user_route", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      userId: {
        type: Sequelize.INTEGER,
        onDelete: "CASCADE",
        references: {
          model: "Users",
          key: "id"
        }
      },
      routeId: {
        type: Sequelize.INTEGER,
        onDelete: "CASCADE",
        references: {
          model: "Routes",
          key: "id"
        }
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("user_route");
  }
};

My controller and route is as following:

  create(req, res) {
    return UserRoutes.create({
      userId: req.body.userId,
      routeId: req.body.routeId
    })
      .then(userRoute => res.status(201).send(userRoute))
      .catch(err => res.status(400).send(err));
  },



app.post("/api/userRoutes", userRoutesController.create);

So when I try to post to that route, I get the error message on the top of the post.

like image 540
leogoesger Avatar asked Nov 15 '17 04:11

leogoesger


2 Answers

I had this same error, and was similarly thrown by an unrelated issue (my SQL clause had single quotes that were all escaped, and, since the query ran when I removed them, I was focused on a way to remove the escaping). My problem was that my migration was generated with a table name that violated the PostgreSQL naming convention; when I corrected this (I had a capitalized first letter in my table name, so I edited the migration for a lowercase first letter and reran it), the error disappeared. Maybe that might fix yours?

like image 168
rainydaymatt Avatar answered Oct 22 '22 02:10

rainydaymatt


After you created db, models and migrations, you need to run

sequelize db:migrate

before running your application.

If not, it will throw SequelizeDatabaseError as mentioned above.

like image 30
Jerry Chong Avatar answered Oct 22 '22 03:10

Jerry Chong