簡體   English   中英

在序列中與hasMany關系工作,表不存在,為什么?

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

在建立正確的關系,我如何建立這種關系以及我的情況下可能出現的問題方面需要您的幫助...

對我而言,sequelize的奇怪之處在於它會創建餐券,並給我錯誤,提示它不存在,我無法理解其背后的邏輯..

如何在這里設置我需要的關系?

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”不存在

同時創建表

為此,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