SequelizeBaseError:列“...”不存在

时间:2017-07-30 17:45:02

标签: javascript node.js postgresql express sequelize.js

使用续集表连接'belongsTo'和'hasMany;

时遇到错误

SequelizeBaseError:列videos.eventEventId不存在。

我在模型或数据库中没有此列,并且没有在路由器中的任何位置引用 - 不知道为什么在我的模型或表中没有这个时会返回此错误?

似乎出现在事件模型上对主键的任何引用上。

  const Sequelize = require('sequelize')   

  // Heroku postgres db connection
  const sequelize = new Sequelize(process.env.DATABASE_URL, {
      dialect: 'postgres',
      protocol: 'postgres',
      define: {
        timestamps: false,
       },
      dialectOptions: {
          ssl: true
      }
  });

  // Connect all the models/tables in the database to a db object,
  //so everything is accessible via one object
  const db = {};

  db.Sequelize = Sequelize;
  db.sequelize = sequelize;

  //Models/tables
  db.venues = require('./models/venue.js')(sequelize, Sequelize);
  db.events = require('./models/event.js')(sequelize, Sequelize);
  db.videos = require('./models/video.js')(sequelize, Sequelize);

  //Relations
  db.events.hasMany(db.videos);
  db.videos.belongsTo(db.events);


  module.exports = db;

模型:事件

  module.exports = (sequelize, DataTypes) => {
    const Event = sequelize.define('event', {
      id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        autoIncrement: true,
        field: 'id',
      },
      venueId: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'venue',
          key: 'id'
        },
        field: 'venue_id'
      },
      eventId: {
        type: DataTypes.TEXT,
        primaryKey: true,
        allowNull: true,
        field: 'event_id'
      },
      eventName: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'event_name'
      },
      startTime: {
        type: DataTypes.TIME,
        allowNull: true,
        field: 'start_time'
      },
      place: {
        type: DataTypes.JSONB,
        allowNull: true,
        field: 'place'
      },
      loc: {
        type: DataTypes.GEOMETRY,
        allowNull: true,
        field: 'loc'
      },
      description: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'description'
      },
      intelIsMusicEvent: {
        type: DataTypes.BOOLEAN,
        allowNull: true,
        field: 'intel_is_music_event'
      },
      intelHashTags: {
        type: 'ARRAY',
        allowNull: true,
        field: 'intel_hash_tags'
      },
      instaLocTag: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'insta_loc_tag'
      },
      isEventOn: {
        type: DataTypes.BOOLEAN,
        allowNull: true,
        field: 'is_event_on'
      },
      videoCount: {
        type: DataTypes.BIGINT,
        allowNull: true,
        field: 'video_count'
      }
    }, {
      tableName: 'event'
    });

    return Event;
  };

视频模型

  module.exports = function(sequelize, DataTypes) {
    return sequelize.define('video', {
      eventId: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'event_id',
        primaryKey: true,
      },
      userName: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'user_name'
      },
      userPhotoLink: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'user_photo_link'
      },
      instaVideoLink: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'insta_video_link'
      },

    }, {
      tableName: 'insta_video'
    });
  };

路由器

    router.get('/venues/search', wrap(function*(req, res) {
           req.checkQuery('q', 'No keyword is specified').notEmpty().isAlpha()
           req.checkQuery('o', 'No keyword is specified').notEmpty().isInt() //o = offset of venue in db

   const result = yield req.getValidationResult()

   if (!result.isEmpty()) {
      res.status(400).json({
         status: 'error',
         errorMessage: result.array()
      })
   }

   try {
      const venues = yield db.events.findAll({
        include: [
                   {
                     model: db.videos,
                   }
                 ],
         raw: true,
         limit: 10,
         offset: `${req.query.o}`,
         order: [ [ 'video_count', 'DESC' ]],
         attributes: ['id', 'eventId', 'eventName', 'startTime', 'place', 'loc', 'description', 'instaLocTag', 'isEventOn', 'videoCount',],
         where: {
           'place.location.city': {
            $ilike: `%${req.query.q}%`
          },
          $or: {
            isEventOn:true
          }
        }
      })

      res.json({status: 'success', payload: venues})
   } catch (err) {
      console.log(err)
      console.trace(err)
   }

    })
  )

  module.exports = router

0 个答案:

没有答案