0
votes

So I have two tables, one with a list of users, then another with associated accounts. So for example, one user might have two accounts. Also, several users might be associated with overlapping accounts.

I have a belongsTo and a hasMany, as I've done in the past.

but when I try to get the associated accounts, I'm getting back an error stating Accounts.id is not found.

Association:

Accounts.belongsTo(Users,{foreignKey: 'uid'})
Users.hasMany(Accounts, {foreignKey: 'uid'})

uid is a primaryKey on the User table. There are no primary keys on the Accounts table.

Models:

Users

db.define('users', {
  uid: {
    type: Sequelize.BIGINT,
    primaryKey: true
  },
  login_count: {
    type: Sequelize.INTEGER,
    defaultValue: 0
  },
  approved: {
    type: Sequelize.BOOLEAN,
    defaultValue: false
  },
  last_login_ip: {
    type: Sequelize.INET,
  }
});

Accounts:

db.define('accounts', {
  uid: {
    type: Sequelize.BIGINT
  },
  account_num: {
    type: Sequelize.BIGINT
  },
  token: {
    type: Sequelize.STRING
  }
});
1
can you add all model definition as well? - Anatoly
done @Anatoly! Added above. - bbrodsky
And where is a model that is a link between users and accounts? - Anatoly
@anatoly I don't have one. I'd think that the primary key in Users matches the uid in accounts. Is that not enough? - bbrodsky
(I just noticed I had the models labeled wrong. It's fixed now.) - bbrodsky

1 Answers