0

I Try to query using sequelize into postgres database, but while querying I get the error called: column "createdAt" does not exist Where table looks like:

    Column     |          Type          |
 id            | integer                |
 title         | character varying(200) |
 technologies  | character varying(200) |
 budget        | character varying(20)  |
 description   | text                   |
 contact_email | character varying(100) |
 createdat     | date                   |
 updatedat     | date                   |

Indexes: "gigs_pkey" PRIMARY KEY, btree (id)

Router : gigs.js

import express from "express";
import db from "../config/database.js";
import Gig from "../models/Gig.js";

const router = express.Router();

router.get("/", async (req, res) => {
  try {
    const gigs = await Gig.findAll();
    console.log(gigs);
    res.sendStatus(200);
  } catch (err) {
    console.log(err);
    res.sendStatus(500);
  }
});
export default router;

Models : Gigs.js


import Sequelize from "sequelize";
import db from "../config/database.js";

const Gig = db.define("gig", {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
  },
  title: {
    type: Sequelize.STRING,
  },
  technologies: {
    type: Sequelize.STRING,
  },
  budget: {
    type: Sequelize.STRING,
  },
  description: {
    type: Sequelize.STRING,
  },
  contact_email: {
    type: Sequelize.STRING,
  },
  createdAt: {
    type: Sequelize.DATEONLY,
  },
  updatedAt: {
    type: Sequelize.DATEONLY,
  },
});

export default Gig;

Sequelize try to query 'createdat' and 'updateat' like this:

SELECT "createdAt", "updatedAt" FROM "gigs" AS "gig";

Which is throwing an error

Executing (default): SELECT "id", "title", "technologies", "budget", "description", "contact_email", "createdAt", "updatedAt" FROM "gigs" AS "gig"

Error looks something loke this:

original: error: column "createdAt" does not exist at Parser.parseErrorMessage (D:\Programming\Database\PostgreSQL\Sequelize\node_modules\pg-protocol\src\parser.ts:369:69) at Parser.handlePacket (D:\Programming\Database\PostgreSQL\Sequelize\node_modules\pg-protocol\src\parser.ts:188:21) at Parser.parse (D:\Programming\Database\PostgreSQL\Sequelize\node_modules\pg-protocol\src\parser.ts:103:30) at Socket. (D:\Programming\Database\PostgreSQL\Sequelize\node_modules\pg-protocol\src\index.ts:7:48) at Socket.emit (node:events:527:28) at Socket.emit (node:domain:475:12) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) at Socket.Readable.push (node:internal/streams/readable:228:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23) { length: 169, severity: 'ERROR', code: '42703', detail: undefined, hint: 'Perhaps you meant to reference the column "gig.createdat".', position: '81', internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'parse_relation.c', line: '3611', routine: 'errorMissingColumn', sql: 'SELECT "id", "title", "technologies", "budget", "description", "contact_email", "createdAt", "updatedAt" FROM "gigs" AS "gig";', parameters: undefined }, sql: 'SELECT "id", "title", "technologies", "budget", "description", "contact_email", "createdAt", "updatedAt" FROM "gigs" AS "gig";', parameters: {} }

2
  • You are talking about postgresql, why do you tag it as mysql?? Commented Sep 5, 2022 at 17:03
  • isn't the column createdat and not createdAt ? Commented Sep 5, 2022 at 17:03

1 Answer 1

0

You need to indicate all field names explicitly that differ from model prop names (even if just by letter case):

  createdAt: {
    type: Sequelize.DATEONLY,
    field: 'createdat',
  },
  updatedAt: {
    type: Sequelize.DATEONLY,
    field: 'updatedat'
  },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.