Trying to create a table Transaction in a postgresql database that references Buyers and Sellers s.t. both are objects from the Users table.
I think I have the migration working to look something like the following:
exports.up = function(knex, Promise) {
return knex.schema.createTable('likes', t => {
t.increments('id').primary()
t.integer('buyers_id').references('users.id').onDelete('CASCADE')
t.integer('sellers_id').references('users.id').onDelete('CASCADE')
...
t.datetime("created_at");
t.datetime("updated_at");
})
};
Next, I need to manage the association in the model, s.t the Transaction belongs to a Buyer and a Seller, which are both members of the User class.
To clarify the question, I am able to create the model with these attributes, but the association does not seem to be working.
here is my Transaction model:
const BaseModel = require("./BaseModel");
// const Password = require('objection-password')();
class Transaction extends BaseModel {
static get tableName() {
return "transactions";
}
static get relationMappings () {
const User = require('./User');
const Item = require('./Item')
return {
buyer: {
relation: BaseModel.BelongsToOneRelation,
modelClass: User,
join: {
from: 'transactions.buyers_id',
to: 'users.id'
}
},
seller: {
relation: BaseModel.BelongsToOneRelation,
modelClass: User,
join: {
from: 'transactions.sellers_id',
to: 'users.id'
}
},
books: {
relation: BaseModel.BelongsToOneRelation,
modelClass: Item,
join: {
from: 'transactions.items_id',
to: 'items.id'
}
}
}
}
}
module.exports = Transaction;
Here is the relevant route where I try to eager load the buyer:
let router = express.Router();
router.get('/', async (req, res) => {
const transactions = await Transaction
.query()
.eager(['buyer', 'items')
res.json(transactions);
});
I have figured this out. The above code works, using the aliases buyers and sellers and associating those two types of Users with Transactions.