Skip to content

Instantly share code, notes, and snippets.

@mvegap
Created March 5, 2020 18:39
Show Gist options
  • Save mvegap/56fd44a1349dcf512b61952284a36c87 to your computer and use it in GitHub Desktop.
Save mvegap/56fd44a1349dcf512b61952284a36c87 to your computer and use it in GitHub Desktop.
model-colaborador.js
module.exports = (sequelize, DataTypes) => {
const Colaborador = sequelize.define('Colaborador', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
usuarioId: {
allowNull: false,
type: DataTypes.INTEGER,
},
nombres: {
allowNull: false,
type: DataTypes.STRING,
},
apellidoPaterno: {
allowNull: false,
type: DataTypes.STRING,
},
apellidoMaterno: {
allowNull: false,
type: DataTypes.STRING,
},
nombresCompletos: {
type: DataTypes.VIRTUAL,
get() {
return `${this.nombres} ${this.apellidoPaterno} ${this.apellidoMaterno}`;
},
},
tipoDocumento: {
allowNull: true,
type: DataTypes.STRING,
},
numDocumento: {
allowNull: true,
type: DataTypes.STRING,
},
fechaNacimiento: {
allowNull: true,
type: DataTypes.DATEONLY,
},
email: {
allowNull: true,
type: DataTypes.STRING,
},
celular: {
allowNull: true,
type: DataTypes.STRING,
},
numEmergencia: {
allowNull: true,
type: DataTypes.INTEGER,
},
direccion: {
allowNull: true,
type: DataTypes.STRING,
},
carnetExtranjeria: {
allowNull: true,
type: DataTypes.INTEGER,
},
estado: {
allowNull: false,
type: DataTypes.INTEGER,
defaultValue: 1, // activo
},
}, {
timestamps: true,
paranoid: true,
version: true,
freezeTableName: true,
});
Colaborador.associate = function (models) {
Colaborador.belongsTo(models.Usuario, {
as: 'Usuario',
foreignKey: 'usuarioId',
targetKey: 'id',
});
Colaborador.hasOne(models.Tutor, {
as: 'Tutor',
foreignKey: 'id',
sourceKey: 'id',
});
};
return Colaborador;
};
@tylrhas
Copy link

tylrhas commented Mar 5, 2020

and try this module.exports = (sequelize, DataTypes) => { const Colaborador = sequelize.define('Colaborador', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: DataTypes.INTEGER, }, usuarioId: { allowNull: false, type: DataTypes.INTEGER, }, nombres: { allowNull: false, type: DataTypes.STRING, }, apellidoPaterno: { allowNull: false, type: DataTypes.STRING, }, apellidoMaterno: { allowNull: false, type: DataTypes.STRING, }, nombresCompletos: { type: DataTypes.VIRTUAL, get() { return ${this.nombres} ${this.apellidoPaterno} ${this.apellidoMaterno}; }, }, tipoDocumento: { allowNull: true, type: DataTypes.STRING, }, numDocumento: { allowNull: true, type: DataTypes.STRING, }, fechaNacimiento: { allowNull: true, type: DataTypes.DATEONLY, }, email: { allowNull: true, type: DataTypes.STRING, }, celular: { allowNull: true, type: DataTypes.STRING, }, numEmergencia: { allowNull: true, type: DataTypes.INTEGER, }, direccion: { allowNull: true, type: DataTypes.STRING, }, carnetExtranjeria: { allowNull: true, type: DataTypes.INTEGER, }, estado: { allowNull: false, type: DataTypes.INTEGER, defaultValue: 1, // activo }, }, { timestamps: true, paranoid: true, version: true, freezeTableName: true, }); Colaborador.associate = function (models) { Colaborador.belongsTo(models.Usuario); Colaborador.hasOne(models.Tutor); }; return Colaborador; };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment