Relationships hasOne and belongsTo in the same model sequelize

0

I'm using sequelize along with nodejs and I'm having difficulty with a relationship.

I have 3 tables where (local, equipment, shed, area) where in one place I have 1 equipment, it is in a shed and belongs to an area.

The templates look like this:

Location:

import { Sequelize, DataTypes } from 'sequelize';

export default (sequelize, dataTypes) => {
const model = sequelize.define('local', {
    id: {
        type: dataTypes.STRING(200),
        primaryKey: true,
        allowNull: false,
        required: true,
        unique: true
    },
    name: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    idEquipment: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    idShed: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    idArea: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    situation: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        required: true,
        defaultValue: true
    },
    capacity: {
        type: dataTypes.FLOAT,
        allowNull: false,
        required: true
    },
    posX: {
        type: dataTypes.STRING,
        allowNull: false,
        required: true
    },
    posY: {
        type: dataTypes.STRING,
        allowNull: false,
        required: true
    },
    posZ: {
        type: dataTypes.STRING,
        allowNull: false,
        required: true
    },
    status: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: true
    }
}).schema('public');

model.associate = (models) => {
    model.belongsTo(models.area, {
        foreignKey: 'idArea'
    });

    model.belongsTo(models.equipment, {
        foreignKey: 'idEquipment'
    });

    // model.belongsTo(models.shed, {
    //     foreignKey: 'idShed'
    // });
};

return model;

};[]

equipment:

import { Sequelize, DataTypes } from 'sequelize';

export default (sequelize, dataTypes) => {
const model = sequelize.define('equipment', {
    id: {
        type: dataTypes.STRING(200),
        primaryKey: true,
        allowNull: false,
        unique: true,
        required: true
    },
    description: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    idArea: {
        type: dataTypes.STRING(200),
        allowNull: true,
        required: false
    },
    idHangar: {
        type: dataTypes.STRING(200),
        allowNull: true,
        required: false
    },
    idControlPlan: {
        type: dataTypes.STRING(200),
        allowNull: true,
        required: false
    },
    dateControlPlan: {
        type: dataTypes.DATE,
        allowNull: true,
        required: false
    },
    idUserControlPlan: {
        type: dataTypes.STRING(200),
        allowNull: true,
        required: false
    },
    status: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: true
    }
}).schema('public');

model.associate = (models) => {
    model.hasOne(models.local, {
        foreignKey: 'idEquipment'
    });
};

return model;
};

shed:

import { Sequelize, DataTypes } from 'sequelize';

export default (sequelize, dataTypes) => {
const model = sequelize.define('shed', {
    id: {
        type: dataTypes.STRING(200),
        primaryKey: true,
        allowNull: false,
        unique: true,
        required: true
    },
    idArea: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    description: {
        type: dataTypes.STRING(200),
        allowNull: false,
        required: true
    },
    status: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: true
    }
}).schema('public');

model.associate = (models) => {
    // model.hasOne(models.local, {
    //     foreignKey: 'idShed'
    // });

    model.belongsTo(models.area, {
        foreignKey: 'idArea'
    });
};

return model;

};

area:

import { Sequelize, DataTypes } from 'sequelize';

export default (sequelize, dataTypes) => {
const model = sequelize.define('area', {
    id: {
        type: dataTypes.STRING(200),
        primaryKey: true,
        allowNull: false,
        unique: true,
        required: true
    },
    description: {
        type: dataTypes.STRING,
        unique: true,
        allowNull: false,
        required: true
    },
    status: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: true
    }
}).schema('public');

model.associate = (models) => {
    model.belongsToMany(models.company, {
        through: 'companyArea',
        foreignKeyConstraint: true,
        foreignKey: 'idArea'
    });

    model.hasOne(models.shed, {
        foreignKey: 'idArea'
    });

    model.hasOne(models.local, {
        foreignKey: 'idArea'
    });
};

return model;

};

When I add the shed relationship, it informs me that there is no relationship, when I shot everything it will normally:

  

[SQL Error]          SequelizeDatabaseError: relation "public.sheds" does not exist          EXIT

     

[SQL Error]          relation "public.sheds" does not exist          EXIT

I'm using postgres database.

Where can the error be, would it be a writing error? Or can not a template have a belongsTo and hasOne relationship at the same time?

    
asked by anonymous 18.10.2018 / 15:15

0 answers