简体   繁体   中英

Working with hasMany relationship in sequelize , table not exist , why?

Need your help with setting the correct relationship , how i make such relationship work and what can be the problem in mine case ...

The strange thing for me about sequelize is that it creates table coupons and give me error that it not exist , i can't understand the logic behind it ..

How to set the relationship that i need here ?

const CustomerCoupon = sequelize.define('customer_coupon',{
  coupon_id:{
    type:Sequelize.BIGINT,
    allowNull:false
  },
  customer_id:{
    type:Sequelize.BIGINT,
    allowNull:false
  }
  },{
  schema:'couponsystem'
  });

const Customer = sequelize.define('customer',{
  customer_id:{
    type:Sequelize.BIGINT,
    primaryKey:true,
    allowNull:false
  },
  nickname: {
    type:Sequelize.STRING(100),
    allowNull:false,
    unique:true
  },
  firstname:{
    type:Sequelize.STRING(100),
    allowNull:false
  },
  lastname:{
    type:Sequelize.STRING(100),
    allowNull:false
  },
  email:{
    type:Sequelize.STRING(150),
    unique:true,
    allowNull:false
  },
  password:{
    type:Sequelize.STRING(150),
    unique:true,
    allowNull:false
  },
  country:{
    type:Sequelize.STRING(100),
    allowNull:false
  }
},{
  schema:'couponsystem',
  underscored:true
});

  const Coupon = sequelize.define('coupon',{
  coupon_id: {
    primaryKey:true,
    type:Sequelize.BIGINT,
    allowNull:false
  },
  title: {
    unique:true,
    type:Sequelize.STRING(50),
    allowNull:false
  },
  release_date: {
    type:Sequelize.DATE,
    release_time:Sequelize.DATE,
    allowNull:false 
  },
  expiration_date:{
    type:Sequelize.DATE,
    allowNull:false
  },
  price: {
    type:Sequelize.DOUBLE,
    allowNull:false
  },
  coupon_type:{
    type:Sequelize.ENUM('Traveling','Food','Camping','Restaurants'),
    allowNull:false
  }
},{
  schema:'couponsystem'
});

const User = sequelize.define('user', {
  user_id: {
    primaryKey:true,
    type:Sequelize.BIGINT,
    allowNull:false 
 },
  nickname: {
    type:Sequelize.STRING(150),
    unique:true,
    allowNull:false
  },
  email: {
    type:Sequelize.STRING(150),
    unique:true,
    allowNull:false
  },
  user_password: {
    type:Sequelize.STRING(150),
    unique:true,
    allowNull:false
  }
}, {
  schema:'couponsystem',
  underscored:true
});

    // CustomerCoupon.removeAttribute('id');
    Coupon.belongsToMany(Customer, {through: CustomerCoupon});
    Customer.belongsToMany(Coupon, {through: CustomerCoupon});

    User.hasOne(Customer,{foreignKey:'user_id'});

Unhandled rejection SequelizeDatabaseError: relation "couponsystem.coupons" does not exist

同时创建表

Sequelize not accept singular table name for this you have to use property called freezeTableName so that it will not make your coupon to coupons

var Coupon = sequelize.define('coupon', { /* bla*/ }, {


  // disable the modification of tablenames; By default, sequelize will automatically
  // transform all passed model names (first parameter of define) into plural.
  // if you don't want that, set the following
  freezeTableName: true,

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM