overlookmotel / sequelize-hierarchy

Nested hierarchies for Sequelize
MIT License
301 stars 90 forks source link

SequelizeHierarchyError: Parent does not exist #193

Closed HenonoaH closed 4 years ago

HenonoaH commented 4 years ago

@overlookmotel Looks like there is a problem with setParent() When I tried to add parent I also ran into this issue.

Code that I used to create the parent

        User.findOne({
            where: { uniqueId: uniqueId }
        }).then(user => {
            if (user) {
                const userHierarchy = new UserHierarchy();
                userHierarchy.setParent(user).then(data => {
                    console.log(data);
                });
            }
        });

UserHierarchy model

import sequelize from 'sequelize';
import sequelizeHierarchy from 'sequelize-hierarchy';

const Sequelize = sequelizeHierarchy(sequelize);

class UserHierarchy extends Sequelize.Model {
    static init(sequelize, { INTEGER }) {
        return super.init(
            {
                userId: INTEGER
            },
            {
                sequelize,
                hierarchy: {
                    freezeTableName: true
                },
                createdAt: 'createdOn',
                updatedAt: 'updatedOn',
                freezeTableName: true
            }
        );
    }
}

export default UserHierarchy;

I may be missing small things over here. It could be great if you suggest to me what am missing.