Answer the question
In order to leave comments, you need to log in
Sequelize does not return a model object. How to fix?
Made a small project on Sequelize/Express with html/css interface, it worked on bare SQL queries. I tried to do it right away on the models, but my request (findAll(), console.log) returned an empty object. SELECT related functions didn't work either. Now I want to try again. Actually made models, arranged connections. But sequelize did not return an object to me, and does not return it. Either the name of the model or an empty object is returned instead. What have I done wrong ?
https://github.com/zeromask1337/ranepadb
Database dump link:
https://drive.google.com/file/d/1EfBtn63SdF_2X8Cun...
const { DataTypes } = require("sequelize");
const db = require("../config/database");
const Office = db.define(
"Office",
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
address: DataTypes.STRING(45),
},
{
// Other model options go here
tableName: "office",
timestamps: false,
}
);
const Employees = db.define(
"Employees",
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: DataTypes.STRING(45),
job: DataTypes.STRING(45),
reg_date: DataTypes.DATEONLY,
salary: DataTypes.DECIMAL(10, 2),
weekend: DataTypes.INTEGER,
office_id: { type: DataTypes.INTEGER, allowNull: false },
},
{
// Other model options go here
tableName: "employees",
timestamps: false,
}
);
// Employees.hasOne(Office, { foreignKey: "office_id" });
Office.belongsTo(Employees, { foreignKey: "office_id" }); // Foreign key
const Developer = db.define(
"Developer",
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
role: DataTypes.STRING(45),
level: DataTypes.STRING(45),
project_count: DataTypes.INTEGER,
},
{
// Other model options go here
tableName: "developer",
timestamps: false,
}
);
Developer.belongsTo(Employees, { foreignKey: "id" }); // Foreign key
const Clients = db.define(
"Clients",
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: DataTypes.STRING(45),
total_sum: DataTypes.DECIMAL(20, 2),
},
{
// Other model options go here
tableName: "clients",
timestamps: false,
}
);
const Projects = db.define(
"Projects",
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
price: DataTypes.DECIMAL(15, 2),
started: DataTypes.DATEONLY,
ended: DataTypes.DATEONLY,
teamlead_id: { type: DataTypes.INTEGER, allowNull: false },
designer_id: { type: DataTypes.INTEGER, allowNull: false },
programmer_id: { type: DataTypes.INTEGER, allowNull: false },
dbarch_id: { type: DataTypes.INTEGER, allowNull: false },
client_id: { type: DataTypes.INTEGER, allowNull: false },
},
{
// Other model options go here
tableName: "projects",
timestamps: false,
}
);
Projects.hasOne(Developer, { foreignKey: "teamlead_id" }); // Foreign key
Projects.hasOne(Developer, { foreignKey: "designer_id" }); // Foreign key
Projects.hasOne(Developer, { foreignKey: "programmer_id" }); // Foreign key
Projects.hasOne(Developer, { foreignKey: "dbarch_id" }); // Foreign key
Projects.hasOne(Clients, { foreignKey: "client_id" }); // Foreign key
module.exports = { Clients, Developer, Employees, Office, Projects };
require("dotenv").config();
const {
Clients,
Developer,
Employees,
Office,
Projects,
} = require("./models/models");
const express = require("express");
const app = require("./req/requests");
//Database
const db = require("./config/database");
//Connecting to database
db.authenticate()
.then(() => console.log("Database connected..."))
.catch((err) => console.log("Error: " + err));
//Synchronize models
db.sync().then(console.log("Synced"));
//Express
app.use(express.static("public"));
app.listen(process.env.PORT, () =>
console.log(`Server started on port ${process.env.PORT}...`)
);
const offices = Office.findAll();
console.log("All offices:", JSON.stringify(offices, null, 2));
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question