Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

id: null when creating a new item in sequelize

When I try to create a new Conversation item Sequelize will return an object with id: null eventhough there is an valid id in the database. How can I get Sequelize to return the last inserted id to the newly created item?

Conversation.create({
  type: 'private',
  createdBy: 1,
}).then(conversation => {
  reply(conversation);
});

Will return

{
  "type": "conversations",
  "id": null,
  "createdBy": 1,
  "created_at": "2016-03-18T01:47:48.000Z"
}

My code:

const Conversation = model.define('Conversation', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
  },
  type: {
    type: Sequelize.ENUM,
    values: ['private', 'group'],
    validate: {
      isIn: ['private', 'group'],
    },
  },
  createdBy: {
    type: Sequelize.INTEGER,
    field: 'created_by',
  },
}, {
  tableName: 'conversations',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: false,
  getterMethods: {
    type: () => 'conversations',
  },
});

const User = model.define('User', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
  },
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name',
    allowNull: false,
  },
  lastName: {
    type: Sequelize.STRING,
    field: 'last_name',
    allowNull: true,
  },
  email: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  profileImg: {
    type: Sequelize.STRING,
    field: 'profile_img',
    allowNull: false,
  },
  password: Sequelize.STRING,
}, {
  tableName: 'users',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  getterMethods: {
    type: () => 'users',
  },
});

Conversation.belongsToMany(User, {
  foreignKey: 'conversation_id',
  otherKey: 'user_id',
  through: 'conversation_user',
  timestamps: false,
});

User.belongsToMany(Conversation, {
  as: 'conversations',
  foreignKey: 'user_id',
  otherKey: 'conversation_id',
  through: 'conversation_user',
  timestamps: false,
});
like image 692
Liam Avatar asked Mar 18 '16 01:03

Liam


People also ask

How do you define an object in Sequelize?

You can use JSON datatype of field in Sequelize (this corresponds to JSON datatype in PostgreSQL). const User = sequelize. define('User', { name: { type: DataTypes. STRING, allowNull: false }, age: { type: DataTypes.

What is required false in Sequelize?

sequelize fails to load a model if a nested include has a where clause that returns no models.. to ensure it doesnt fail completely, you can set a required: false clause along with the where clause.. this makes sequelize return a blank array instead of failing the load completely.. Save this answer.

How do you create a Sequelize?

A very useful shortcut: the create method​ Sequelize provides the create method, which combines the build and save methods shown above into a single method: const jane = await User.create({ name: "Jane" }); // Jane exists in the database now!


1 Answers

Yo need to put autoIncrement: true in id field:

id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  }

Personally I would advice to skip the id column as sequalize does it automatically for you and works nicely.

hope it helps :)

like image 157
FastTurtle Avatar answered Oct 27 '22 20:10

FastTurtle