abelosorio / sequelize-views-support

Adds VIEWS support to Sequelize
https://www.npmjs.com/package/sequelize-views-support
MIT License
28 stars 11 forks source link

View couldn't be created #16

Open wondie opened 4 years ago

wondie commented 4 years ago

I couldn't create a view. Below is my code.

Model.js


module.exports = (sequelize, DataTypes) => {
    const VwBackscattering = sequelize.define('vw_backscattering', {
            capture_datetime:  DataTypes.DATE,
            bb_470nm:DataTypes.DOUBLE,
            bb_532nm:DataTypes.DOUBLE,
            bb_650nm: DataTypes.DOUBLE,
            boat_datetime:  DataTypes.DATE,
            rainfall: DataTypes.DOUBLE,
            temperature: DataTypes.DOUBLE,
            latitude: DataTypes.DOUBLE,
            longitude: DataTypes.DOUBLE
        },
        {
            treatAsView: true,
            viewDefinition: `
              CREATE VIEW "vw_backscattering" AS
            'SELECT t1.*, t2.*,
            'ST_SetSRID(ST_MakePoint(t2.longitude, t2.latitude), 4326) as point_geom
            'FROM backscatterings t1 INNER JOIN 
            '    ancillary_data t2 ON t1.capture_datetime = t2.boat_datetime;`
        });
    return VwBackscattering;
};

index.js


const Sequelize = require('sequelize-views-support');
const view = require('./models/VwBackscattering');
const VwBackscattering = view(db, Sequelize.DataTypes)
VwBackscattering.sync().then(() => {
    console.log('VwBackscattering table created');
});
module.exports = VwBackscattering;
babthamotharan commented 2 years ago

Hi, Use syncView function to create view

const Sequelize = require('sequelize-views-support');
const view = require('./models/VwBackscattering');
const VwBackscattering = view(db, Sequelize.DataTypes)
VwBackscattering.syncView().then(() => {
    console.log('VwBackscattering table created');
});
module.exports = VwBackscattering;