< Express

Express - Táblák kapcsolása

Szerző: Sallai András

Copyright © 2025, Sallai András

Licenc: CC Attribution-Share Alike 4.0 International

Web: https://szit.hu

Táblák

employees(id, name, city, salary, rankId)
ranks(id, name)

Model

import { DataTypes } from 'sequelize' import sequelize from '../database/database.js' import Rank from './rank.js' const Employee = sequelize.define('employee', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }, name: { type: DataTypes.STRING, allowNull: false }, city: { type: DataTypes.STRING, allowNull: true }, salary: { type: DataTypes.DOUBLE, allowNull: true }, rankId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Rank, key: 'id' } } }) Employee.belongsTo(Rank, { foreignKey: 'rankId' }) sequelize.sync({ force: false }) export default Employee

Kontroller

A dolgozók lekérdezéséhez kapcsoljuk a beosztásokat.

const employees = await Employee.findAll({ include: { model: Rank, attributes: ['name'] } })