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

Relation between Models not working. #98

Closed
subodhpareek18 opened this issue Mar 21, 2017 · 9 comments
Closed

Relation between Models not working. #98

subodhpareek18 opened this issue Mar 21, 2017 · 9 comments

Comments

@subodhpareek18
Copy link

subodhpareek18 commented Mar 21, 2017

Have been trying to follow the demo given here many-to-many-sequelize by @ekryski. But to no avail ☹...

I have two models:

// channel-model.js
import Sequelize from 'sequelize';

export default (sequelize) => {
  const Channel = sequelize.define('Channel', {
    id: {
      type: Sequelize.DataTypes.INTEGER,
      autoIncrement: true,
      allowNull: false,
      primaryKey: true
    },
    name: {
      type: Sequelize.DataTypes.STRING,
      allowNull: false
    },
    createdAt: {
      type: Sequelize.DataTypes.DATE,
      defaultValue: new Date()
    },
    updatedAt: {
      type: Sequelize.DataTypes.DATE,
      defaultValue: new Date()
    }
  }, {
    freezeTableName: true,
    classMethods: {
      associate() {
        Channel.hasOne(sequelize.models.Message, {
          foreignKey: 'id',
          targetKey: 'id',
          as: 'message'
        });
      }
    }
  });

  return Channel;
};
// message-model.js
import Sequelize from 'sequelize';

export default (sequelize) => {
  const Message = sequelize.define('Message', {
    id: {
      type: Sequelize.DataTypes.INTEGER,
      autoIncrement: true,
      allowNull: false,
      primaryKey: true
    },
    text: {
      type: Sequelize.DataTypes.STRING,
      allowNull: false
    },
    createdAt: {
      type: Sequelize.DataTypes.DATE,
      defaultValue: new Date()
    },
    updatedAt: {
      type: Sequelize.DataTypes.DATE,
      defaultValue: new Date()
    }
  }, {
    freezeTableName: true
  });

  return Message;
};

I am able to create both resources independently, but not able to associate a given message to a channel. Here I want any one message to be linked to any channel. All queries being made to the channel just ignore the data and do not modify any relational fields, which btw do not even appear in the resource on the get call.

I'm not sure where the error lies. Maybe in the migrations? But they were not documented in the demo above so not sure if I've written them correctly or not.

Have uploaded my entire code here: feathers-sequelize-poc for further evaluation by everyone.

@ekryski
Copy link
Member

ekryski commented Mar 22, 2017

@zusamann if you are just getting started I would use the new generator as mentioned here: #20 (comment).

It sets them up correctly for you now 😄 . The feathers-demos repos are starting to become stale and are in need of some TLC with all the updates that have gone into the Auk release.

If that works for you can we close this? 🙏

@subodhpareek18
Copy link
Author

@ekryski fair enough, let me go try it out and I'll re-open the issue if I can't get it to work?

@subodhpareek18
Copy link
Author

subodhpareek18 commented Mar 24, 2017

@ekryski I have tried it with feathers-cli and I am able to reproduce the error. For further verification I did another test where I used models and migrations written in the official example by sequelize for express.

What is happening is the data I send in the query for the relational field. It's somehow not getting processed and being set to null. I have noticed this in all my tests.

So let's say I have a customer with a relational field of contactId. And in customer/1 I do a PATCH of

{ "contactId": 1 }

The error response I see is null value in column "contactId" violates not-null constraint and also in the SQL query I see logged on the server there is no mention of this relational/reference field being updated.

Could it be a bug recently introduced? Using the following versions right now, must all be latest:

{
    "feathers": "^2.1.1",
    "feathers-hooks": "^1.8.1",
    "feathers-rest": "^1.7.1",
    "feathers-sequelize": "^1.4.3",
    "nodemon": "^1.11.0",
    "pg": "^6.1.5",
    "pg-hstore": "^2.3.2",
    "sequelize": "^3.30.2"
}

@subodhpareek18
Copy link
Author

I have updated my sample repo here to showcase the exact same issue.

I have two resources here, a Channel and a Message, and just for this example I have only one message in each channel.

Now I'm able to create messages no problem.

// POST /messages { "text": "Hola!" }
{
  "createdAt": "2017-03-24T14:00:26.577Z",
  "updatedAt": "2017-03-24T14:00:26.577Z",
  "id": 1,
  "text": "Hola!",
  "ChannelId": null
}

Which is all good, but when I now try creating a new channel like so:

// POST localhost:3003/channels { "name": "hoo hoo lalala", "messageId": 1 }

Here is the SQL query that runs

INSERT INTO "Channel" ("id","name","createdAt","updatedAt") VALUES (DEFAULT,'hoo hoo lalala','2017-03-24 14:00:26.587 +00:00','2017-03-24 14:00:26.587 +00:00') RETURNING *;

As you can see, no mention of messageId in the above. Which is why I get this error:
SequelizeBaseError: null value in column "messageId" violates not-null constraint

@subodhpareek18
Copy link
Author

Oh oh oh, I think it's a bug introduced in the recent version.

I downgraded from: "feathers-sequelize": "^1.4.3"
to: "feathers-sequelize": "^1.4.0"

And not facing the same issue anymore.

@daffl
Copy link
Member

daffl commented Mar 24, 2017

Interesting. Could you try at which version (1.4.1, 1.4.2 or 1.4.3) it breaks? Eventually the caret (^) range should still install the latest version.

@subodhpareek18
Copy link
Author

subodhpareek18 commented Mar 24, 2017

sure, let me try 1.4.1 and 1.4.2 and get back to you

@subodhpareek18
Copy link
Author

I could get it to work with the version 1.4.3 as well.
The issue was with the relations written in the migration.

My apologies.

@joeblubaugh
Copy link

@zusamann Can you give some examples of the problems with your relations? I'm encountering a similar issue and understanding the model problems could help me too.

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

4 participants