Srajan Gupta
Srajan Gupta

Reputation: 41

Using sqlite3 with sequelize to build a desktop app using electron. Only few tables are getting created during production

everyone. I have encountered a very strange bug. I am using sqlite3 to create a desktop app along with sequelize as an ORM. I tried to build my app today for the first time and installed the setup which was created. The app only created a few of the tables that I had programmed. The app is supposed to create the following tables.

enter image description here

But when running in production mode. Then it creates only the following tables.

enter image description here

The table "items" and all the tables involving itemId as a foreign key are missing. I am not sure which part of the code will be relevant to share, so I am sharing the link to the Git where I am updating the project. https://github.com/Srajan1/my-shop-app.

I have also checked the sequelize logs in the debugging mode and they seem to be working fine and creating all the tables. enter image description here

Upvotes: 0

Views: 2599

Answers (1)

Srajan Gupta
Srajan Gupta

Reputation: 41

Turns out I had not defined the Item model the right way. This is what I should have done.

const { Model, DataTypes } = require("sequelize");
const sequelize = require("../database/db");
const Metric = require("./metricModel");

const Item = sequelize.define(
  "Item",
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    hsn: {
      type: DataTypes.STRING
    },
    available: {
      type: DataTypes.FLOAT,
      defaultValue: 0,
    },
    incoming: {
      type: DataTypes.FLOAT,
      defaultValue: 0,
    },
  },
  {
    sequelize,
    tableName: "items",
    freezeTableName: true,
  }
);

Item.belongsTo(Metric, { foreignKey: "metricId", targetKey: "id" });
module.exports = Item;

Upvotes: 1

Related Questions