如何使用 Knex.js 和 Bookshelf.js (ExpressJS/Postgress) 插入一对多
How to insert one to many with Knex.js and Bookshelf.js (ExpressJS/Postgress)
我正在尝试在用户注册时在两个 table 上创建一条记录。
user.js
const db = require('../database');
const User = db.Model.extend({
tableName: 'login_user',
hasSecurePassword: true,
hasTimestamps: true,
team : () =>{
return this.hasMany('Team', 'owner_id');
}
});
module.exports = User;
team.js
const db = require('../database');
const Team = db.Model.extend({
tableName: 'team_master',
hasTimestamps: true,
user: () => {
return this.belongsTo('User', 'owner_id');
},
});
module.exports = Team;
knex 迁移文件
exports.up = function (knex, Promise) {
return knex.schema.createTable('login_user', t => {
t.increments('id').unsigned().primary();
t.string('email').notNull();
t.string('password_digest').notNull();
t.string('fName').notNull();
t.string('lName').notNull();
t.timestamp('created_at').defaultTo(knex.fn.now())
t.timestamp('updated_at').defaultTo(knex.fn.now())
})
.createTable('team_master', t => {
t.increments('id').unsigned().primary();
t.integer('owner_id').references('id').inTable('login_user');
t.string('teamName').notNull();
t.timestamp('created_at').defaultTo(knex.fn.now())
t.timestamp('updated_at').defaultTo(knex.fn.now())
});
};
exports.down = function (knex, Promise) {
return knex.schema.dropTable('login_user').dropTable('team_master');
};
我的插入代码如下所示
const user = new User({
email: req.body.email,
password: req.body.password,
fName: req.body.fName,
lName: req.body.fName,
//teamName: req.body.teamName,
});
user.save().then(() => {
res.send('User Created');
});
所以在这种情况下,我想要做的是将 teamName
插入到 team_master
table 中,并将新创建的唯一用户 ID 插入到 owner_id
中 team_master
table.
有人可以为我指出正确的方向吗?谢谢。
您应该能够使用从保存的用户生成的 ID 来填充团队,如下所示:
user.save()
.then(user => {
// user.id should be populated with the generated ID
return new Team({
owner_id: user.id,
// set your other team properties
}).save()
})
.then(team => {
// do something with team
})
我正在尝试在用户注册时在两个 table 上创建一条记录。
user.js
const db = require('../database');
const User = db.Model.extend({
tableName: 'login_user',
hasSecurePassword: true,
hasTimestamps: true,
team : () =>{
return this.hasMany('Team', 'owner_id');
}
});
module.exports = User;
team.js
const db = require('../database');
const Team = db.Model.extend({
tableName: 'team_master',
hasTimestamps: true,
user: () => {
return this.belongsTo('User', 'owner_id');
},
});
module.exports = Team;
knex 迁移文件
exports.up = function (knex, Promise) {
return knex.schema.createTable('login_user', t => {
t.increments('id').unsigned().primary();
t.string('email').notNull();
t.string('password_digest').notNull();
t.string('fName').notNull();
t.string('lName').notNull();
t.timestamp('created_at').defaultTo(knex.fn.now())
t.timestamp('updated_at').defaultTo(knex.fn.now())
})
.createTable('team_master', t => {
t.increments('id').unsigned().primary();
t.integer('owner_id').references('id').inTable('login_user');
t.string('teamName').notNull();
t.timestamp('created_at').defaultTo(knex.fn.now())
t.timestamp('updated_at').defaultTo(knex.fn.now())
});
};
exports.down = function (knex, Promise) {
return knex.schema.dropTable('login_user').dropTable('team_master');
};
我的插入代码如下所示
const user = new User({
email: req.body.email,
password: req.body.password,
fName: req.body.fName,
lName: req.body.fName,
//teamName: req.body.teamName,
});
user.save().then(() => {
res.send('User Created');
});
所以在这种情况下,我想要做的是将 teamName
插入到 team_master
table 中,并将新创建的唯一用户 ID 插入到 owner_id
中 team_master
table.
有人可以为我指出正确的方向吗?谢谢。
您应该能够使用从保存的用户生成的 ID 来填充团队,如下所示:
user.save()
.then(user => {
// user.id should be populated with the generated ID
return new Team({
owner_id: user.id,
// set your other team properties
}).save()
})
.then(team => {
// do something with team
})