Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

node.js - Sequelize - insert data from select value as foreign id

I can't add a new Rent - association table, I have something like that: Now i have error. I don't know why the client_id is null there

"_id":"","clients":"1","cars":"1","dataOd":"2020-12-07","dataDo":"2021-01-11"} (node:14200) UnhandledPromiseRejectionWarning: SequelizeValidationError: notNull Violation: Rent.client_id cannot be null, notNull Violation: Rent.car_id cannot be null, notNull Violation: Rent.dateOd cannot be null

piece of bulkCreate method, and it works

{client_id: allClients[0]._id, car_id: allCars[0]._id, dateOd: '2001-01-01', dateDo: '2009-01-01'}, {client_id: allClients[1]._id, car_id: allCars[0]._id, dateOd: '2001-02-01', dateDo: '2009-02-01'},

const Rent = sequelize.define('Rent', {
_id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true
},
client_id: {
    type: Sequelize.INTEGER,
    allowNull: false
},
car_id: {
    type: Sequelize.INTEGER,
    allowNull: false
},dateOd: {
    type: Sequelize.DATE,
    allowNull: false
},

connections:

Client.hasMany(Rent, {as: 'rents', foreignKey: {name: 'client_id', allowNull: false}, constraints: true, onDelete: 'CASCADE'});
Rent.belongsTo(Client, {as: 'client', foreignKey: {name: 'client_id', allowNull: false} } );
Car.hasMany(Rent, {as: 'rents', foreignKey: {name: 'car_id', allowNull: false}, constraints: true, onDelete: 'CASCADE'});
Rent.belongsTo(Car, {as: 'car', foreignKey: {name: 'car_id', allowNull: false} });

Method for routing

exports.showAddRentForm = (req, res, next) => {
let allClients, allCars;
ClientRepository.getClients()
    .then(clients => {
        allClients = clients;
        return CarRepository.getCars();
    })
    .then(cars => {
        allCars = cars;
        res.render('pages/rent/form', {
            rent: {},
            formMode: 'createNew',
            allClients: allClients,
            allCars: allCars,
            formAction: '/rent/add',
        });
    });

input form, car looks same

 <label for="clients">Client:<abbr title="required" aria-label="required">*</abbr></label>
    <select id="clients" name="clients" required <%= (formMode == 'showDetails') ? 'disabled' : ''%> >
        <% for (let i=0; i<allClients.length; i++) { let client = allClients[i]; %>
        <option value="<%= client._id %>" label="<%= client.firstName + ' ' + client.lastName %>"
                <%= (rent._id && client._id.toString() == rent.client._id.toString()) ? 'selected' : ''%>></option>
        <% } %>
    </select>
<label for="dataOd" >Data od:<abbr title="required" aria-label="required">*</abbr></label>
    <input type="date" name="From" id="From" value="<%= rent.dataOd%>"
            <%= (formMode == 'showDetails') ? 'disabled' : '' %>/>
    <span id="errorDataOd" class="errors-text"></span>

Error code i received

Executing (default): SELECT `Rent`.`_id`, `Rent`.`client_id`, `Rent`.`car_id`, `Rent`.`dateOd`, `Rent`.`dateDo`, `Rent`.`createdAt`, `Rent`.`updatedAt`, `client`.`_id` AS `client._id`, `client`.`firstName` AS `client.firstName`, `client`.`lastName` AS `client.lastName`, `client`.`lastName2` AS `client.lastName2`, `client`.`city` AS `client.city`, `client`.`pesel` AS `client.pesel`, `client`.`createdAt` AS `client.createdAt`, `client`.`updatedAt` AS `client.updatedAt`, `car`.`_id` AS `car._id`, `car`.`marka` AS `car.marka`, `car`.`model` AS `car.model`, `car`.`proDate` AS `car.proDate`, `car`.`createdAt` AS `car.createdAt`, `car`.`updatedAt` AS `car.updatedAt` FROM `Rents` AS `Rent` LEFT OUTER JOIN `Clients` AS `client` ON `Rent`.`client_id` = `client`.`_id` LEFT OUTER JOIN `Cars` AS `car` ON `Rent`.`car_id` = `car`.`_id`;
question from:https://stackoverflow.com/questions/65844735/sequelize-insert-data-from-select-value-as-foreign-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...