oktatas:web:back-end_framework:express:tablak_kapcsolasa
Tartalomjegyzék
Express - Táblák kapcsolása
- Szerző: Sallai András
- Copyright © 2025, Sallai András
- Web: https://szit.hu
Táblák
employees(id, name, city, salary, rankId) ranks(id, name)
Model
- employees.js
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'] } })
oktatas/web/back-end_framework/express/tablak_kapcsolasa.txt · Utolsó módosítás: 2025/03/28 21:02 szerkesztette: admin