Skip to content

associations from official documentation example #292

Open
@eugene-matvejev

Description

@eugene-matvejev

What was unclear/insufficient/not covered in the documentation

I came from PHP, doctrine world, but now try understand sequlize, I struggle with associations, I tried to follow manual http://docs.sequelizejs.com/manual/associations.html

If possible: Provide some suggestion how we can enhance the docs

more examples

idea is to have more than one battlefield associated with each game. but only one game to each battlefield, e.g. one-to-many

#game.js
export default (sequelize, DataTypes) => {
    const model = sequelize.define(
        'Game',
        {
            id: {
                // type: DataTypes.UUID,
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
        },
        {
            tableName: 'games',
            timestamps: false,
        }
    );

    return model;
};
#battlefield.js
export default (sequelize, DataTypes) => {
    const model = sequelize.define(
        'Battlefield',
        {
            id: {
                // type: DataTypes.UUID,
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            game: {
                // type: DataTypes.UUID,
                type: DataTypes.INTEGER,
                allowNull: false,
            }
        },
        {
            tableName: 'battlefields',
            timestamps: false,
        }
    );

    model.associate = function ({ Game, Battlefield, User }) {
        Game.hasMany(Battlefield, { foreignKey: 'game', sourceKey: 'id' });

        Battlefield.belongsTo(Game, { foreignKey: 'game', targetKey: 'id' });
    }

    return model;
};
#query:
                models.Battlefield.findAll({
                    include: [
                        {
                            model: models.Game,
                            where: {
                                id: gameId,
                            },
                        },
                    ],
                    raw: true,
                })
result: 
Game is not associated to Battlefield!

to overcome it, index.js is like very needed:

import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
import { database as c } from '../../config/config';

const sequelize = new Sequelize(c.database, c.username, c.password, c);

const basename = path.basename(__filename);
const context = fs
    .readdirSync(__dirname)
    .reduce(
        (acc, file) => {
            if ((file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')) {
                const model = sequelize['import'](path.join(__dirname, file));
                acc[model.name] = model;
            }

            return acc;
        },
        {}
    );

for (const name in context) {
    const model = context[name];

    if (model.associate) {
        model.associate(context);
    }
}

context.sequelize = sequelize;
context.Sequelize = Sequelize;

export default context;

it is unclear from docs, how to initiate models

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions