I have seed data for agents, listings and potential buyers for migration files using knex. I created a join table called listingsBuyers referencing the foreign key for listings and buyers. Is there a way to seed a join table referencing the the foreign key for listings and buyers? The way it is set up below is giving an error listing not defined.
exports.up = function(knex) {
return knex.schema.createTable('listings', table =>{
table.increments()
table.string('address')
table.integer('agent_id')
table.foreign('agent_id').references('agents.id')
})
};
exports.down = function(knex) {
return knex.schema
.dropTable('listings')
};
exports.up = function(knex) {
return knex.schema.createTable('buyers', table =>{
table.increments()
table.string('name')
table.integer('listing_id')
table.foreign('listing_id').references('listings.id')
})
};
exports.down = function(knex) {
return knex.schema
.dropTable('buyers')
};
this is the join table
exports.up = function(knex) {
return knex.schema.createTable('listingsBuyers', table =>{
table.increments()
table.integer('listing_id')
table.foreign('listing_id').references('listings.id')
table.integer('buyer_id')
table.foreign('buyer_id').references('buyers.id')
})
};
exports.down = function(knex) {
return knex.schema
.dropTable('listingsBuyers')
};
the seed file
exports.seed = function(knex) {
return knex('listingsBuyers').alter([
{listing_id: listings.id , buyer_id: buyers.id},
//{listing_id: 2, buyer_id: 1},
//{listing_id: 3, buyer_id: 3}
])
.then(() =>{//set the value for auto-incrementing
return knex.raw(`SELECT setval('listingsBuyers_id_seq', (SELECT MAX(id) FROM listingsBuyers))`)
});
};