0

I get this error "RequestError: Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column.".

My database schema looks something like this ,

class Owner extends Model {}
Owner.init({
  name: Sequelize.STRING,
  }, { sequelize,timestamps: false });

class Hotel extends Model {}
Hotel.init({
    name: Sequelize.STRING,
  }, { sequelize,timestamps: false });


  class Users extends Model {}
  Users.init({
    name: Sequelize.STRING
  }, { sequelize,timestamps: false });

class User_Hotel extends Model {}
User_Hotel.init({
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      }
    }, { sequelize,timestamps: false });

class Hotel_Owner extends Model {}
Hotel_Owner.init({
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      }
    }, { sequelize,timestamps: false });

Owner.belongsToMany(Hotel, { through: { model: Hotel_Owner, unique: false } });
Hotel.belongsToMany(Owner, { through: { model: Hotel_Owner, unique: false } });


Hotel.belongsToMany(Users, { through: { model: User_Hotel, unique: false } });
Users.belongsToMany(Hotel, { through: { model: User_Hotel, unique: false } });



Now, I have to insert data in my tables, which I do something like this.

hotel = Hotel.create({name:"WestIn"});
[owner,created] = Owner.findOrCreate({where:{name:"John Doe"}});

Hotel_Owner.create({OwnerId: owner.id, HotelId: hotel.id });

user = Users.create({name:"Common man"});

User_Hotel.create({ HotelId: hotel.id , UsersId:user.id});

I am not sure , why I am getting this error.

4
  • Hi, you tagged your question mysql, but you mention MSSQL (a common abbreviation of Microsoft SQL Server) in your question. I have never seen the error message come from MySQL, but it seems to be well known for Microsoft SQL Server, see for example stackoverflow.com/questions/10262426/… Commented Aug 26, 2022 at 23:22
  • Show generated SQL that causes the error Commented Aug 27, 2022 at 9:32
  • Cannot reproduce your error. I will note that most of Sequelize's methods return Promises so you should be awaiting just about everything, e.g.: hotel = await Hotel.create({name:"WestIn"}); Commented Aug 27, 2022 at 11:14
  • Yeah,, I actually found the error. The association tables that I have explicitly made, do not contain fields for Hoteld and UserId , which is why I was getting that Error. The error message is not appropriate. It is no where related to timestamps, rather it signifies that the column I am trying to insert doesn't exist in the table. Commented Aug 28, 2022 at 10:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.