简体   繁体   中英

Foriegn key relation (NodeJS REST API with sequalize ORM)

I'm trying to create a rest a REST API with NodeJS . Am using MySql as the database and Using sequelize ORM.

I was trying to fetch data by joining two tables using findOne .

Attaching the table structure below: 表接触说明

表contact_email的描述

The contact table has one to many relationship with contact_email table.

Below is the sequelize model for theese tables:

contact.model.js

const Contact = sequelize.define('contact', {
    contact_id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        unique: true,
        primaryKey: true
    },
    user_id: {
        type: DataTypes.INTEGER,
        references: { model: User, key: 'user_id' }
    },
    first_name: {
        type: DataTypes.STRING
    },
    last_name: {
        type: DataTypes.STRING
    },
    nick_name: {
        type: DataTypes.INTEGER,
    },
    dob: {
        type: DataTypes.DATE
    },
    address: {
        type: DataTypes.STRING,
    },
    city: {
        type: DataTypes.STRING,
    },
    state: {
        type: DataTypes.STRING,
    },
    country: {
        type: DataTypes.STRING,
    },
    zip_code: {
        type: DataTypes.STRING,
    },
    status: {
        type: DataTypes.INTEGER,
        defaultValue: 1
    },
    create_date: {
        type: DataTypes.DATE
    },
    update_date: {
        type: DataTypes.DATE
    }
},
    {
        tableName: 'contact',
        createdAt: 'create_date',
        updatedAt: 'update_date'        
    }
);




const ContactEmail = sequelize.define('ContactEmail', {
    contact_id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        references: { model: Contact, key: 'contact_id' }
    },
    email: {
        type: DataTypes.STRING,
        primaryKey: true
    }
},
    {
        tableName: 'contact_email',
        timestamps: false
    }
);



Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact)

module.exports = { Contact, ContactEmail, ContactPhone };

Following is the sequelize function to fetch the data:

contact.service.js

const Contact = await ContactModel.Contact.findOne({
        where: {
            contact_id: contactId
        },
        include: [{
            model: ContactModel.ContactEmail,
            as: 'emails'
        }]
    })

when I call the API, It throws an error from the data

Unknown column 'emails.contactContactId' in 'field list'

The auto-generated query is as follows:

SELECT `contact`.`contact_id`, `contact`.`user_id`, `contact`.`first_name`, `contact`.`last_name`, `contact`.`nick_name`, `contact`.`dob`, `contact`.`address`, `contact`.`city`, `contact`.`state`, `contact`.`country`, `contact`.`zip_code`, `contact`.`status`, `contact`.`create_date`, `contact`.`update_date`, `emails`.`contact_id` AS `emails.contact_id`, `emails`.`email` AS `emails.email`, `emails`.`contactContactId` AS `emails.contactContactId` FROM `contact` AS `contact` LEFT OUTER JOIN `contact_email` AS `emails` ON `contact`.`contact_id` = `emails`.`contactContactId` WHERE `contact`.`contact_id` = '5';

My table doesn't have a column contactContactId . It should be contact_id . Does anyone know the solution for this?

Thanks in advance.

// import the models Contact & ContactEmail

const contact = await Contact.findOne({
        where: {
            contact_id: contactId
        },
        include: [{
            model: ContactEmail,
            as: 'emails'
        }]
 })
const Contact = sequelize.define('contact', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    unique: true,
    primaryKey: true
  },
  user_id: {
    type: DataTypes.INTEGER,
    references: { model: User, key: 'user_id' }
  },
  first_name: {
    type: DataTypes.STRING
  },

  // ........

  create_date: {
    type: DataTypes.DATE
  },
  update_date: {
    type: DataTypes.DATE
  }
},
  {
    tableName: 'contact',
    createdAt: 'create_date',
    updatedAt: 'update_date'
  }
);




const ContactEmail = sequelize.define('ContactEmail', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
  },
  contact_id: {
    type: DataTypes.INTEGER,
    references: { model: Contact, key: 'id' },
    onUpdate: "CASCADE",
    onDelete: "CASCADE"
  },
  email: {
    type: DataTypes.STRING
  }
},
  {
    tableName: 'contact_email',
    timestamps: false
  }
);



Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact)

module.exports = { Contact, ContactEmail, ContactPhone };

ContactEmail should have the same foreignKey option just like Contact has:

Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact, , { foriegnKey: 'contact_id' })

Also you can look at my other asnwer on a similar question

The issue has been solved by modifying the modal as follows.

contact.model.js

const Contact = sequelize.define('contact', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    unique: true,
    primaryKey: true,
    field : 'contact_id'
  },
  user_id: {
    type: DataTypes.INTEGER,
    references: { model: User, key: 'user_id' }
  },
  first_name: {
    type: DataTypes.STRING
  },

  // ........

  create_date: {
    type: DataTypes.DATE
  },
  update_date: {
    type: DataTypes.DATE
  }
},
  {
    tableName: 'contact',
    createdAt: 'create_date',
    updatedAt: 'update_date',
    underscored : true
  }
);

Added field: 'contact_id' to field description and chnaged the name ti id for the and added underscored: true property for model.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM