Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get it work with hasMany+belongsTo associations. #2

Open
vladuva opened this issue Oct 4, 2015 · 3 comments
Open

Cannot get it work with hasMany+belongsTo associations. #2

vladuva opened this issue Oct 4, 2015 · 3 comments

Comments

@vladuva
Copy link

vladuva commented Oct 4, 2015

I have two models: User and Trade and I have these associations:

User.hasMany(Trade);
Trade.belongsTo(User);

I get blueprint error when starting Sails:

node_modules/sails-hook-sequelize-blueprints/index.js:338
              var alias = foreign.as || foreign.name || foreign;
                                 ^

TypeError: Cannot read property 'as' of undefined

If I explicitly define foreignKey:

User.hasMany(Trade, { foreignKey: { name: 'User', allowNull: false }});

I get a Sequelize error:

node_modules/sequelize/lib/associations/helpers.js:7
    throw new Error(
    ^

Error: Naming collision between attribute 'User' and association 'User' on model Trade. To remedy this, change either foreignKey or as in your association definition

Did I do something wrong or is there something wrong with blueprints?

@cesardeazevedo
Copy link
Owner

Thanks for the feedback,

This hook uses the alias to match the association, you should specify the alias as for the foreign key.

Take a look at this samples

https://github.com/cesardeazevedo/sails-hook-sequelize-blueprints/blob/master/test/fixtures/sampleapp/api/models/image.js#L15-L22

https://github.com/cesardeazevedo/sails-hook-sequelize-blueprints/blob/master/test/fixtures/sampleapp/api/models/user.js#L19-L27

Let me know if you got it.

@vladuva
Copy link
Author

vladuva commented Oct 6, 2015

I did not. I extended the definition according to your example:

User.hasMany(Trade, {
  as: 'Trades',
  foreignKey: {
    name: 'UserId',
    as: 'Trades',
  },
});
Trade.belongsTo(User, {
  as: 'User',
  foreignKey: {
    name: 'UserId',
    as: 'User',
  },
});

There are no errors during startup, but there's an error when I try /users/list"

SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'Trades.daySequence' in 'field list'

Do I still do something wrong?

Why sails-hook-sequelize-blueprints cannot just work with Sequelize default assignment setup?

@cesardeazevedo
Copy link
Owner

It's seems there's other error causing this.

this hook doesn't loads the sequelize actually, https://github.com/festo/sails-hook-sequelize does it

there's something wrong with daySequence column, have you force the sequelize syncronization at config/models.js?

module.exports.models = {
    migrate: 'drop'
}

can i have a full sample to reproduce it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants