dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Self-Referenced Many To Many #165

Open asaf050 opened 8 years ago

asaf050 commented 8 years ago

Hello,

I'm trying to create a self-referenced many to many relationship but I keep getting 404 error. I don't know if i'm doing something wrong or epilogue doesn't support it.

Here's the relevant code from my project:

// Define sequelize models
var AssemblyPart = sequelize.define('assemblypart', {
    quantity: {
        type: DataTypes.FLOAT,
        allowNull: false,
        validate: {
            notEmpty: true, // don't allow empty strings
            min: 0,
            isDecimal: true
        }
    }
});
var Part = sequelize.define('part', {
    part_number: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
            notEmpty: true, // don't allow empty strings
        }
    },
    description: {
        type: DataTypes.STRING
    }
}, {
    paranoid: true,
    indexes: [
        // Create a unique index on part number
        {
            unique: true,
            fields: ['part_number']
        }
    ]
});

// Set up the relationship
Part.belongsToMany(Part, {
    as: 'ParentPartId',
    foreignKey: 'ParentPartId',
    through: AssemblyPart
});
Part.belongsToMany(Part, {
    as: 'ChildrenPartId',
    foreignKey: 'ChildrenPartId',
    through: AssemblyPart
});

// Setup the resources
epilogue.resource({
    model: Part,
    endpoints: ['/part', '/part/:id'],
    associations: true
});
epilogue.resource({
    model: AssemblyPart,
    associations: true
});
mbroadst commented 8 years ago

@asaf050 support for this is currently not implemented: https://github.com/dchester/epilogue/issues/34#issuecomment-126927625

asaf050 commented 8 years ago

@mbroadst thank you for your answer. There is any workaround for the meantime?

mbroadst commented 8 years ago

@asaf050 I'm not sure what you mean by "workaround"? Support is not implemented for auto associating self-referencing belongs-to-many relations, the workaround would be to implement support for that feature.

Antiavanti commented 8 years ago

:+1: It will be really nice feature.