So I have a database where I am trying to establish a one-to-many relationship between Supplier and TreeSeed, whereby one Supplier can sell many TreeSeed. I have looked at the sequelize documentation and I have added some utility functions. It seems from the terminal output that Supplier data is inserted but the TreeSeed data is not inserted and the seed_id is set to null. There is a foreign key reference where seedcompany_id references supplier_id.
// utility_funcs/utilites.js
//creates an object with supplier_name being the key and value being the supplier_id
function createSupplierRef(suppliersData){
const supplierLookup = {}
const supplierDataCopy = [...suppliersData]
const creationLookup = supplierDataCopy.map((supplier)=>{
const supplierCpy = {...supplier}
supplierLookup[supplierCpy.dataValues.supplier_name] = supplierCpy.dataValues.supplier_id
})
// e.g{fothergill : 14}
return supplierLookup
}
//swaps out seedcompany_name for seed_company_id
function addSupplierIdToSeeds(treeSeedData,supplierLookup){
const newSeedArray = treeSeedData.map((seed)=>{
const seedCpy = {...seed}
seedCpy.seedcompany_id = supplierLookup[seedCpy.seedcompany_name]
delete seedCpy.seedcompany_name
return seedCpy
})
return newSeedArray
}
module.exports = {createSupplierRef,addSupplierIdToSeeds}
//tree_seeds.js
const {DataTypes, Model} = require("sequelize");
const sequelize = require("../config/sequelize.js")
const treeSeedData = [
{
common_name:"Common Hazel/Cob-Nut",
species_name:"Corylus avellana",
family:"Betulaceae",
seed_cost_incl_VAT:2.18,
quantity_in_stock:14,
seed_number_per_pack:8,
colour:"mixed",
hardiness:"hardy",
soil_type:"chalk/sand/loam",
soil_acidity:"alkaline/neutral",
description:"A small shrubby tree found in mixed woodlands and hedgerows known for its long yellow catkins that appear in Spring and then produces hazelnuts in Summer. It is native to the UK.",
tree_image1:`${__dirname}/tree_photos/hazel1.png`,
tree_image2:`${__dirname}/tree_photos/hazel2.jng`,
tree_image3:`${__dirname}/tree_photos/hazel3.jng`,
seedcompany_name:"Gracey Seed-Co Ltd"
//image(s) collected
},
{
common_name:"Common Alder",
species_name:"Alnus glutinosa",
family:"Betulaceae",
seed_cost_incl_VAT:3.22,
quantity_in_stock:10,
seed_number_per_pack:234,
colour:"green",
hardiness:"hardy",
soil_type:"clay/chalk/loam",
soil_acidity:"alkaline/neutral/acid",
description:"Common in wetland forests, fens and riversides. It can improve soil fertility with its ability to fix nitrogen. Used to make timber veneers, pulp and plywood.",
tree_image1:`${__dirname}/tree_photos/alnus_glutinosa1.jpg`,
tree_image2:`${__dirname}/tree_photos/alnus_glutinosa2.jpg`,
tree_image3: null,
seedcompany_name:"Gracey Seed-Co Ltd"
//image(s) collected
},
{
common_name:"Silver Birch",
species_name:"Betula pendula",
family:"Betulaceae",
seed_cost_incl_VAT:2.75,
quantity_in_stock:6,
seed_number_per_pack:1680,
colour:"mixed",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"It is native to Northern Europe and is found on light and dry soils. Woodpeackers commonly nest in the trunks of this tree. It is a hardwood used in plywood production, toys and wooden back of brushes.",
tree_image1:`${__dirname}/tree_photos/betula_pendula1.jpg`,
tree_image2:`${__dirname}/tree_photos/betula_pendula2.jpg`,
tree_image3:`${__dirname}/tree_photos/betula_pendula3.jpg`,
seedcompany_name:"Fothergill Seeds Ltd"
//image(s) collected
},
{
common_name:"Cherry Dogwood/Cornelian Cherry",
species_name:"Cornus mas",
family:"Cornaceae",
seed_cost_incl_VAT:3.20,
quantity_in_stock:7,
seed_number_per_pack:19,
colour:"yellow/orange",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"It is a multi-stemmed shrub/tree that is notable for being disease-resistant. The fruit produced has skin that is unpleasant with a grainy and bitter taste. They are native to North America, Asia and Europe. It is strong and sturdy and these properties make it a good building material.",
tree_image1:`${__dirname}/tree_photos/cornus_mas1.jpg`,
tree_image2:`${__dirname}/tree_photos/cornus_mas2.jpg`,
tree_image3:`${__dirname}/tree_photos/cornus_mas3.jpg`,
seedcompany_name:"Chiltern Seeds"
//image(s) collected
},
{
common_name:"Sugar Maple",
species_name:"Acer sacchurum",
family:"Aceraceae",
seed_cost_incl_VAT:2.99,
quantity_in_stock:20,
seed_number_per_pack:18,
colour:"mixed",
hardiness:"hardy",
soil_type:"sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"The leaf of the tree is the national emblem of Canada; known for producing a sweet maple syrup and is native to eastern North America. Leaves have three to five lobes and show various shades of gold to scarlet in autumn. It produces greenish yellow flowers in Spring.",
tree_image1:`${__dirname}/tree_photos/acer_saccharum1.jpg`,
tree_image2:`${__dirname}/tree_photos/acer_saccharum2.jpg`,
tree_image3: null,
seedcompany_name:"D.T. Brown Seeds"
//image(s) collected
},
{
common_name:"Black Mulberry",
species_name:"Morus nigra",
family:"Moraceae",
seed_cost_incl_VAT:2.65,
quantity_in_stock:20,
seed_number_per_pack:288,
colour:"mixed",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"A species native to west Asia that is cultivated for its fruit. It produces dark-green heart shaped leaves and is deciduous. It is low maintenance, attracts pollinators and improves soil health.",
tree_image1: `${__dirname}/tree_photos/morus_nigra1.jpg`,
tree_image2: `${__dirname}/tree_photos/morus_nigra2.jpg`,
tree_image3: `${__dirname}/tree_photos/morus_nigra3.jpg`,
seedcompany_name:"D.T. Brown Seeds"
//image(s) collected
},
{
common_name:"Lemonade Tree/Baobob",
species_name:"Adansonia digitata",
family:"Bombacaceae",
seed_cost_incl_VAT:3.10,
quantity_in_stock:15,
seed_number_per_pack:6,
colour:"green",
hardiness:"tender",
soil_type:"sand/loam",
soil_acidity:"alkaline/neutral/acid",
description:"This tree produces short stubby branches from the top of a swollen water-containing trunk. They grow in 32 African countries and can live up to 5000 years. Its pendulous flowers are pollinated by bush babies and bats, its young leaves are edible and produces a woody fruit.",
tree_image1:`${__dirname}/tree_photos/boabab1.jpg`,
tree_image2:`${__dirname}/tree_photos/boabab2.jpg`,
tree_image3:null,
seedcompany_name:"Chiltern Seeds"
//image(s) collected
},
{
common_name:"Common Hornbeam",
species_name:"Carpinus betulus",
family:"Betulaceae",
seed_cost_incl_VAT:2.85,
quantity_in_stock:20,
seed_number_per_pack:27,
colour:"mixed",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"Found in southern and easter England; it produces catkins in late Spring and winged seeds can be spotted in autumn and are dispersed by the wind.It has a twisted trunk with tooth-edged leaves. Commonly it is used for flooring and furniture.",
tree_image1:`${__dirname}/tree_photos/carpinus_betulus1.jpg`,
tree_image2:`${__dirname}/tree_photos/carpinus_betulus_catkins2.jpg`,
tree_image3:null,
seedcompany_name:"Chiltern Seeds"
//image(s) collected
},
{
common_name:"Argyle Apple/Mealy Stringybark",
species_name:"Eucalyptus cinerea",
family:"Myrtaceae",
seed_cost_incl_VAT:2.90,
quantity_in_stock:16,
seed_number_per_pack:26,
colour:"greyish-white",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"neutral/acid",
description:"This is an evergreen tree with a striking canopy of blue-silver foliage. It is tolerant to UK climate, prefers slightly acidic soil and is used as an astringent and antiseptic.",
tree_image1:`${__dirname}/tree_photos/eucalyptus_cinerea1.jpg`,
tree_image2:`${__dirname}/tree_photos/eucalyptus_cinerea2.jpg`,
tree_image3:null,
seedcompany_name:"Kings Seeds"
//image(s) collected
},
{
common_name:"Dawn Redwood",
species_name:"Metasequoia glyptostroboides",
family:"Pinaceae",
seed_cost_incl_VAT:3.30,
quantity_in_stock:13,
seed_number_per_pack:70,
colour:"mixed",
hardiness:"hardy",
soil_type:"chalk/sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"A native tree of China but grows comfortably in UK climates. Mostly planted as an ornamental tree and is a fast growing deciduous tree with needle-like green leaves that turn copper-red when shed in autumn.",
tree_image1:`${__dirname}/tree_photos/dawn_redwood1.jpg`,
tree_image2:`${__dirname}/tree_photos/dawn_redwood2.jpg`,
tree_image3:`${__dirname}/tree_photos/dawn_redwood3.jpg`,
seedcompany_name:"D.T. Brown Seeds"
//image(s) collected
},
{
common_name:"Norway Maple",
species_name:"Acer platanoides",
family:"Aceraceae",
seed_cost_incl_VAT:1.92,
quantity_in_stock:10,
seed_number_per_pack:20,
colour:"yellow/orange",
hardiness:"hardy",
soil_type:"sand/loam/clay",
soil_acidity:"alkaline/neutral/acid",
description:"This tree is found expansively across Europe and Asia and is a common ornamental landscape plant. When they grow they produce a lot of shade that makes it hard for plants and grass underneath to grow.",
tree_image1:`${__dirname}/tree_photos/acer_platanoides1.jpg`,
tree_image2:`${__dirname}/tree_photos/acer_platanoides2.jpg`,
tree_image3:`${__dirname}/tree_photos/acer_platanoides3.jpg`,
seedcompany_name:"Moles Seeds Ltd"
},
{
common_name:"Cootamundra Wattle",
species_name:"Acacia baileyana",
family:"Leguminosae/Fabaceae",
seed_cost_incl_VAT:1.34,
quantity_in_stock:26,
seed_number_per_pack:24,
colour:"yellow",
hardiness:"hardy/half-hardy",
soil_type:"sand/loam",
soil_acidity:"neutral/acid",
description:"This is a tree/shrub with smooth grey-to-brown bark and blue-grey foliage. It is planted in parks and gardens as a wind-break and shading tree.",
tree_image1:`${__dirname}/tree_photos/acacia_baileyana1.jpg`,
tree_image2:`${__dirname}/tree_photos/acacia_baileyana2.jpg`,
tree_image3: null,
seedcompany_name:"Moles Seeds Ltd"
//image(s) collected
},
];
class TreeSeed extends Model {
}
TreeSeed.init({
seed_id:{ //seed_id IS NOT RECOGNIZED
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
unique : true,
allowNull: false
},
common_name:{
type: DataTypes.STRING(45),
unique: true,
allowNull: false
},
species_name:{
type: DataTypes.STRING(45),
unique:true,
allowNull: false
},
family:{
type: DataTypes.STRING(45),
allowNull: false
},
seed_cost_incl_VAT:{
type: DataTypes.DOUBLE(4,2),
allowNull: false
},
quantity_in_stock:{
type: DataTypes.INTEGER,
allowNull: false
},
seed_number_per_pack:{
type: DataTypes.INTEGER,
allowNull: false
},
colour:{
type: DataTypes.STRING(20),
allowNull: false
},
hardiness:{
type: DataTypes.STRING(20),
allowNull: false
},
soil_type:{
type: DataTypes.STRING(60),
allowNull: false
},
soil_acidity:{
type: DataTypes.STRING(60),
allowNull: false
},
description:{
type: DataTypes.TEXT,
allowNull: false
},
tree_image1:{
type: DataTypes.BLOB,
allowNull: false
},
tree_image2:{
type: DataTypes.BLOB,
},
tree_image3:{
type: DataTypes.BLOB,
},
seedcompany_id:{
type: DataTypes.INTEGER,
references:{
model: 'Supplier',
key: 'supplier_id'
}
}
}
,{
sequelize,
timestamps: false,
freezeTableName: true
})
module.exports = {TreeSeed,treeSeedData};
supplier.js
const {DataTypes, Model} = require("sequelize");
const sequelize = require("../config/sequelize.js")
const suppliersData = [
{
supplier_name: "Kent & Stowe" //1
},
{
supplier_name: "Spear & Jackson" //2
},
{
supplier_name: "Wilkinson Sword" //3
},
{
supplier_name: "Darlac Ltd" //4
},
{
supplier_name: "Walensee" //5
},
{
supplier_name: "Evergreen Garden Care Ltd" //6
},
{
supplier_name: "Neilsen" //7
},
{
supplier_name: "Flymo" //8
},
{
supplier_name: "Burgon & Ball" //9
},
{
supplier_name: "Colwelt" //10
},
{
supplier_name: "Alina" //11
},
{
supplier_name: "GreFusion" //12
},
{
supplier_name: "GardenGloss" //13
},
{
supplier_name: "Nutscene" //14
},
{
supplier_name: "Gracey Seed-Co Ltd" //15
},
{
supplier_name: "Fothergill Seeds Ltd" //16
},
{
supplier_name: "Chiltern Seeds" //17
},
{
supplier_name: "D.T. Brown Seeds" //18
},
{
supplier_name: "Kings Seeds" //19
},
{
supplier_name: "Moles Seeds Ltd" //20
},
{
supplier_name: "Miracle Gro" //21
},
{
supplier_name: "Jamieson Brothers" //22
},
{
supplier_name: "Tree Appeal" //23
},
{
supplier_name: "Noel Tatt" //24
},
{
supplier_name: "DV Design" //25
},
{
supplier_name: "Woodmansterne" //26
},
{
supplier_name: "Abacus Cards" //27
},
{
supplier_name: "Nokular Limited" //28
},
{
supplier_name: "Purple Fox" //29
},
]
class Supplier extends Model {
}
Supplier.init({
supplier_id:{
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
field: 'supplier_id'
},
supplier_name:{
type: DataTypes.STRING(60),
allowNull: false
},
},{
sequelize,
timestamps: false,
freezeTableName: true,
})
module.exports = {Supplier,suppliersData};
//models/index.js
const {TreeSeed,treeSeedData} = require("./tree_seeds.js")
const {Supplier,suppliersData} = require("./supplier.js");
const { Model } = require("sequelize");
Supplier.hasMany(TreeSeed,{
foreignKey: 'seedcompany_id'
});
TreeSeed.belongsTo(Supplier,{
foreignKey: 'seedcompany_id'
})
module.exports = {TreeSeed,treeSeedData,Supplier,suppliersData};
The important actions occur in server.js
//server.js
const sequelize = require("./config/sequelize.js")
const {TreeSeed,Supplier,treeSeedData,suppliersData} = require("./models")
const {createSupplierRef,addSupplierIdToSeeds} = require("./utility_funcs/utilities.js")
const authenticate = async () => {
try{
await sequelize.authenticate();
console.log("authentication successful")
}catch(error){
console.error('Unable to connect to the database:',error)
}
}
authenticate();
const makeModels = async () => {
try{
await sequelize.sync({force:true});
console.log("All models made")
}catch(error){
console.error('Unable to connect to make tables:',error)
}
};
makeModels();
const insertSuppliers = async () =>{
try{
const records = await Supplier.bulkCreate(suppliersData,{validate:true})
// console.log("Suppliers data successfully inserted:",records)
return records
}catch(error){
// console.error("Could not insert supplier records")
return [];
}
};
const insertTreeSeed = async (suppliers) =>{
try{
//swap out logic
console.log("supplier((",suppliers)
const supplierRefObj = createSupplierRef(suppliers)
// console.log("supplierref:",supplierRefObj)
const newTreeSeedData = addSupplierIdToSeeds(treeSeedData,supplierRefObj)
console.log(newTreeSeedData)
await TreeSeed.bulkCreate(newTreeSeedData)//PROBLEM OCCURS ON THIS LINE HERE
// console.log("Treeseed data successfully inserted",records)
}catch(error){
console.error("Could not insert tree seed records")
}
}
insertSuppliers().then((suppliers)=>insertTreeSeed(suppliers))
When I console.log(newTreeSeedData) I get the following which is what I expect (
//part of output shown
{
common_name: 'Cootamundra Wattle',
species_name: 'Acacia baileyana',
family: 'Leguminosae/Fabaceae',
seed_cost_incl_VAT: 1.34,
quantity_in_stock: 26,
seed_number_per_pack: 24,
colour: 'yellow',
hardiness: 'hardy/half-hardy',
soil_type: 'sand/loam',
soil_acidity: 'neutral/acid',
description: 'This is a tree/shrub with smooth grey-to-brown bark and blue-grey foliage. It is planted in parks and gardens as a wind-break and shading tree.',
tree_image1: '/home/mbans8a1/Northcoders/northcoders/projects/arboretum_project/models/tree_photos/acacia_baileyana1.jpg',
tree_image2: '/home/mbans8a1/Northcoders/northcoders/projects/arboretum_project/models/tree_photos/acacia_baileyana2.jpg',
tree_image3: null,
seedcompany_id: 20
}
However at the bulkCreate for TreeSeed the data refuses to insert so I am guessing it is an issue with recognition of primary key seed_id based off of the terminal output.
//part of the output (note the various NULLs in the VALUES where the seed_id should be)
Executing (default): INSERT INTO `TreeSeed` (`seed_id`,`common_name`,`species_name`,`family`,`seed_cost_incl_VAT`,`quantity_in_stock`,`seed_number_per_pack`,`colour`,`hardiness`,`soil_type`,`soil_acidity`,`description`,`tree_image1`,`tree_image2`,`tree_image3`,`seedcompany_id`) VALUES (NULL,'Common Hazel/Cob-Nut','Corylus avellana','Betulaceae','2.18',14,8,'mixed','hardy','chalk/sand/loam','alkaline/neutral','A small shrubby tree found in mixed woodlands and hedgerows known for its long yellow catkins that appear in Spring and then produces hazelnuts in Summer. It is native to the UK.',X'2f686f6d652f6d62616e733861312f4e6f727468636f646572732f6e6f727468636f646572732f70726f6a656374732f6172626f726574756d5f70726f6a6563742f6d6f64656c732f747265655f70686f746f732f68617a656c312e706e67',X'2f686f6d652f6d62616e733861312f4e6f727468636f646572732f6e6f727468636f646572732f70726f6a656374732f6172626f726574756d5f70726f6a6563742f6d6f64656c732f747265655f70686f746f732f68617a656c322e6a6e67',X'2f686f6d652f6d62616e733861312f4e6f727468636f646572732f6e6f727468636f646572732f70726f6a656374732f6172626f726574756d5f70726f6a6563742f6d6f64656c732f747265655f70686f746f732f68617a656c332e6a6e67',15)
The Supplier data insert has a similar message but that data actually gets inserted.