在序列中与hasMany关系工作,表不存在,为什么?

时间:2019-01-21 06:03:40

标签: javascript sequelize.js

在设置正确的关系,我如何使这种关系起作用以及在我的情况下可能出现的问题方面需要您的帮助...

对我来说,关于序列化的奇怪之处在于,它创建了表优惠券,并给我一个错误,即它不存在,我无法理解其背后的逻辑..

如何在这里设置我需要的关系?

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'});
  
    

未处理的拒绝SequelizeDatabaseError:关系“ couponsystem.coupons”不存在

  

created tables in the same time

1 个答案:

答案 0 :(得分:-1)

Sequelize不接受单表名称,您必须使用称为FrozenTableName的属性,这样它才不会使优惠券成为优惠券

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,

})