WDI-SEA / project-2-issues

0 stars 0 forks source link

db.event.create + POST ROUTE METHOD does not autoincrement table id #15

Closed dryutsun closed 2 years ago

dryutsun commented 2 years ago

What's the problem you're trying to solve?

I am attempting to input new entries into my events table.

Post any code you think might be relevant (one fenced block per file)

events.js (controller)

    db.project.findAll({
        where: { userId: res.locals.currentUser.id}
    })
    .then((project) => {
        res.render('./events/new.ejs', {project:project})
    })
    .catch((error) => {
        console.error
        res.status(400).send("404")
    })

})

router.post('/new', (req,res)=>{
    db.event.create({
        title: req.body.title,
        date: req.body.date,
        timeStart: req.body.timeStart,
        locationLat: req.body.locationLat,
        locationLon: req.body.locationLon,
        association: req.body.association,
        sourcedata: req.body.sourcedata,
        comments: req.body.comments,
        userId: res.locals.currentUser.id
    })
    .then((events)=>{
    res.redirect(`./projects/${event.projectId}`)
    })
})

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

Executing (default): INSERT INTO "events" ("id","title","date","timeStart","locationLat","locationLon","association","sourcedata","comments","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING "id","title","date","timeStart","locationLat","locationLon","association","sourcedata","comments","entityId","projectId","createdAt","updatedAt";
/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/dialects/postgres/query.js:344
          return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields });
                 ^

UniqueConstraintError [SequelizeUniqueConstraintError]: Validation error
    at Query.formatError (/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/dialects/postgres/query.js:344:18)
    at Query.run (/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/dialects/postgres/query.js:87:18)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/sequelize.js:619:16
    at async PostgresQueryInterface.insert (/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/dialects/abstract/query-interface.js:734:21)
    at async event.save (/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/model.js:3954:35)
    at async Function.create (/home/t3nj1n/general_assembly/project2/timemap/node_modules/sequelize/lib/model.js:2207:12) {
  errors: [
    ValidationErrorItem {
      message: 'id must be unique',
      type: 'unique violation',
      path: 'id',
      value: '3',
      origin: 'DB',
      instance: event {
        dataValues: {
          id: null,
          title: 'Cat Spotted Near JFK',
          date: 2021-11-16T00:00:00.000Z,
          timeStart: '2:04',
          locationLat: '40.71444693208757',
          locationLon: '-73.99820987115571',
          association: 'catwatch',
          sourcedata: 'Mr. Tang',
          comments: 'Attendant mentioned a cat that was dragging around a snakeskin suitcase. ',
          updatedAt: 2021-11-15T19:13:55.488Z,
          createdAt: 2021-11-15T19:13:55.488Z
        },

This is unexpected behavior because I believe everytime I try and create a new entry using the db create method, it should automatically increment to the next available int. However, in this instance it says that it is null.

What is your best guess as to the source of the problem? Something in my controller is wrong or missing.

What things have you already tried to solve the problem? Tried to figure out if it was a routing error. Tried to see if there was anything in my EJS that was tripping up the POST method. I looked into my models and migrations folder and they both indicate to me that autoincrementation should be working.

tkolsrud commented 2 years ago

promise was not completing, so the data was never actually inserted into db