overlookmotel / sequelize-hierarchy

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

SequelizeHierarchyError: Parent does not exist #194

Closed HenonoaH closed 4 years ago

HenonoaH commented 4 years ago

@overlookmotel @vbogdanov 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 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.

Originally posted by @HenonoaH in https://github.com/overlookmotel/sequelize-hierarchy/issues/75#issuecomment-549917538

overlookmotel commented 4 years ago

.setParent() acts on an instance which is already instantiated in the database. i.e. it's like .update(). You're trying to use it on an instance which isn't in the database yet.

Instead just set parentId when creating the instance.

UserHierarchy.create( { parentId: uniqueId } )
  .then( userHierarchy => {
    // Do what you want with userHierarchy
  } );
overlookmotel commented 4 years ago

Also, I'm not sure what you're doing with the UserHierarchy model.

Usually, if you're trying to make a hierarchy of users, you would be setting parentId on the User model.

Please see the docs here.

HenonoaH commented 4 years ago

.setParent() acts on an instance which is already instantiated in the database. i.e. it's like .update(). You're trying to use it on an instance which isn't in the database yet.

Instead just set parentId when creating the instance.

UserHierarchy.create( { parentId: uniqueId } )
  .then( userHierarchy => {
    // Do what you want with userHierarchy
  } );

Thanks for the prompt response @overlookmotel I thought setParent() could work on a new instance. Now, I'm clear with the hierarchy create & update. Also, this is merely my suggestion we can improve this doc much better.

HenonoaH commented 4 years ago

Also, I'm not sure what you're doing with the UserHierarchy model.

Usually, if you're trying to make a hierarchy of users, you would be setting parentId on the User model.

Please see the docs here.

So, I am having User & UserHierarchy model. UserHierarchy model holds the following attributes

  1. id
  2. userId
  3. level
  4. parentId

Here userId refers to User model.

overlookmotel commented 4 years ago

I don't think there's anything remaining actionable in this issue.

The docs could I suppose have more detail added, but the accessors work exactly the same as in Sequelize itself, so that's already covered in Sequelize's docs. I don't want to make the docs unreadable by becoming too long.

So closing...