简体   繁体   English

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

[英]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 .. 对我而言,sequelize的奇怪之处在于它会创建餐券,并给我错误,提示它不存在,我无法理解其背后的逻辑..

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 未处理的拒绝SequelizeDatabaseError:关系“ couponsystem.coupons”不存在

同时创建表

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 为此,Sequelize不接受单表名称,您必须使用称为freezeTableName的属性,以便它不会使优惠券成为优惠券

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,

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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