Sequelize Error: Sequelizedatabaseerror

Asked

Viewed 14 times

0

Hello, I am building my application on Node and using postgres database. I come across the following error:

SequelizeDatabaseError: column \"zipcode\" of relation \"users\" does not exist"

My model looks like this:

const { Model, DataTypes } = require('sequelize');

class Addresses extends Model {
    static init(connection) {
        super.init({
            zipcode: DataTypes.STRING,
            street: DataTypes.STRING,
            number: DataTypes.INTEGER,
        }, {
            sequelize: connection,
        })
    }

    static associate(models) {
        this.belongsTo(models.User, { foreignKey: 'user_id' });
    }
}

module.exports = Addresses;

My controller is as follows:

const User = require('../models/User');
const Adresses = require('../models/Adresses');

module.exports = {
    async store(req, res) {

        const { user_id } = req.params;
        const { zipcode, street, number } = req.body;

        const user = await User.findByPk(user_id);

        try{

            if(!user) {
                return res.status(400).json({error: 'User not found'})
            }

            const address = await Addresses.create({
                zipcode,
                street,
                number,
                user_id,
            });

            return res.json(address);

        }catch(err) {
            return res.status(400).json({error: 'Algo deu errado: '+err});
        }
    }
};

The routes:

const express = require('express');

const UserController = require('./controllers/UserController');
const AddressesController = require('./controllers/AddressesController');

const routes = express.Router();

routes.get('/users', UserController.index);
routes.post('/users', UserController.store);

routes.post('/users/:user_id/adresses', AddressesController.store);

module.exports = routes;

And the call of each function for insertion and connection to the bank:

const Sequelize = require('sequelize');
const dbConfig = require('../config/database');

const User = require('../models/User');
const Addresses = require('../models/Addresses');

const connection = new Sequelize(dbConfig);

User.init(connection);
Addresses.init(connection);

Addresses.associate(connection.models);

module.exports = connection;

The database is set up to the standard because I have already run the Migrations and everything worked out. In the database, the table Addresses was created as follows by Sequelize:

CREATE TABLE IF NOT EXISTS public.addresses
(
    id integer NOT NULL DEFAULT nextval('addresses_id_seq'::regclass),
    user_id integer NOT NULL,
    zipcode character varying(255) COLLATE pg_catalog."default" NOT NULL,
    street character varying(255) COLLATE pg_catalog."default" NOT NULL,
    "number" integer NOT NULL,
    created_at timestamp with time zone NOT NULL,
    updated_at timestamp with time zone NOT NULL,
    CONSTRAINT addresses_pkey PRIMARY KEY (id),
    CONSTRAINT addresses_user_id_fkey FOREIGN KEY (user_id)
        REFERENCES public.users (id) MATCH SIMPLE
        ON UPDATE CASCADE
        ON DELETE CASCADE
)

TABLESPACE pg_default;

ALTER TABLE public.adresses
    OWNER to postgres;

I’m trying to make a record by Insomnia en route http://localhost:3333/users/1/Addresses passing the following json:

{
    "zipcode": "----",
    "street": "-------",
    "number": ---
}

Plus the Insomnia return me this error Sequelizedatabaseerror: column "zipcode" of relation "users" does not exist", being that in my model, I made the association.

Can you help me? Thanks in advance...

  • Next, the error is clear: column zipcode users does not exist my answer, comes with a question, can show what returns your address?

  • I’ll send you right over.

  • Is that the code you want? 
const { Model, DataTypes } = require('sequelize');

class Addresses extends Model {
 static init(connection) {
 super.init({
 zipcode: DataTypes.STRING,
 street: DataTypes.STRING,
 number: DataTypes.INTEGER,
 }, {
 sequelize: connection,
 })
 }

 static associate(models) {
 this.belongsTo(models.User, { foreignKey: 'user_id' });
 }
}

module.exports = Addresses;
 @Jhowbhz

  • No... you could debug the application and see what’s in the object address?

  • 1

    I have already found the error. When I called my model this way: const Addresses = require('. /models/Adresses) missing a d. The right thing would be const Addresses = require('. /models/Addresses). By the help, but the error was of Portuguese itself.

  • 1

    Can you close the question for me please?

  • :D Solved! like it.

  • @Cesar4ugusto Your question is in the analysis queue for closing, but you can remove it. Read more here: Which is better, let close the question or remove?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.