Is it necessary to create a separate Sequelize model file for each table in my database?

While I've made progress in my journey to learn node-express + mysql + sequelize since day 0, I still find myself questioning if I'm on the right path.

In designing User role models, each user is associated with only one role model.

Some questions that arise are:

  1. Do I need to create a separate sequelize model file for each table?

  2. I came across the concept of normalizing database tables. Should I avoid adding a 'role_id' column in my Users table and consider creating a new table instead?

My scheme design includes the following tables (not entirely sure if this is the correct term, but I created a visual representation using ):

  1. Users table
  2. Roles table
  3. UserRoles table

USERS contains id(pk), full_name, email.

ROLES contains id(pk), description, user_id(foreign key????)

USERROLES contains id(pk), user_id, role_id

Answer №1

  1. A great advantage of organizing your models in a models folder is the ease of access - simply locate the file with the corresponding model name.

    • Additionally, by storing all models in a centralized location, you can efficiently register them all at once in sequelize by scanning through the files in the models folder.
  2. For linking users and roles in a many-to-many relationship, it is essential to have a join-table.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ng-model assigns the value to the select

Below is the angularjs directive I have created: dicApp.directive('listpanel', function ($resource) { return { restrict: 'E', template: '<select ng-model="selectedDictionary" ng-change="listChange()">&apo ...

What's the deal with eval() function?

There has been a lot of talk about the dangers of using the eval() function in HTML/JavaScript programming. While I want to pass in a string to have it read as a variable name, I am aware of the risks associated with using eval(). It seems like the functio ...

I'm looking for a solution to reorganize my current state in order to display the image URL

My React component, which also utilizes TypeScript, is responsible for returning a photo to its parent component: import React, { useEffect, useState } from "react"; import axios from "axios"; export const Photo = () => { const [i ...

Navigating a FormData object in javascript on a .NET WebAPI version 5.2.2

I am currently working on integrating webcam video recording upload using the example provided in this RecordRTC GitHub repo. However, I have encountered a compiling error when trying to use "Request.Files" as indicated in the screenshot below. The error ...

Strangely behaving animated mobile navigation glitch

CodePen showcasing the mobile navigation Upon initial interaction with the mobile navigation, it operates as expected. However, a bug arises with subsequent interactions. The navigation either opens and closes instantly or the links appear in a jumbled or ...

Does anyone know the ins and outs of how the website www.nikebetterworld.com was created?

Hello, I am new to web development and I am interested in creating a page similar to the style of nikebetterworld. Can you point me in the right direction on where to start studying to achieve this design? My impression is that it involves multiple scrol ...

What is the proper way to implement autocomplete in a JavaScript code?

I am attempting to implement autocompletion within JavaScript code, but I'm unsure how to integrate it into a JavaScript table row. Here is where the element is created: Note: $t serves as an incrementing element. $products_specification_ajax ...

`I'm encountering issues when trying to pass an array through localStorage into a new array`

This is a complex and detailed question that I am struggling to find a solution for. Despite using deprecated mysql due to hosting limitations, the problem lies elsewhere. Part 1 involves dataLoader.php, which queries the database and retrieves posx and p ...

What is the best way to save the current time in a database using JavaScript in an ASP.NET application?

I positioned the Label outside the update panel so that any error or confirmation messages would be displayed on the top right side without reloading the entire page. However, the label is not displaying any text. When I comment out the update panel, it wo ...

The error message states: "This is not a CombinedVueInstance type when using Vue with TypeScript

Most of the time, when I use this inside a method on my vue object, it correctly gets the type CombinedVueInstance. However, there are instances where it is assigned types like Vue when accessing this in a method, and Accessors<DefaultComputed> when ...

Validating Arrays of Input Elements using JQuery

I have a task to validate an array of input text elements (mileage). Here is an example of what I need to accomplish: <tbody> <c:forEach items="${list}" var="item"> <tr> <!--some other columns---> ...

Enhancing Dropdown Selections with Jquery and AngularJS

Is there a way to dynamically change the options in this dropdown menu? For example, could I update it to show <li><a data-action="100">100</a> when another button is clicked using ng-click()? <ul class="dropdown-menu pull-right" r ...

I'm having trouble understanding why I can't access the properties of a class within a function that has been passed to an Angular

Currently, I have integrated HTML 5 geolocation into an Angular component: ... export class AngularComponent { ... constructor(private db: DatabaseService) {} // this function is linked to an HTML button logCoords(message, ...

eliminating attributes from a JavaScript object

Seeking a method to strip object properties before sending them to the front-end. Why does this code work as intended: var obj = { name: 'cris', age: 22, } console.log(obj) //displays name and age delete obj.name console.log(obj) //dis ...

Generating a PHP two-dimensional array by linking MySQL tablesorForming a

I'm in need of assistance with a technical issue. Here are the structures of my 2 tables: Courses +------------+--------------+ | Field | Type | +------------+--------------+ | id | int(11) | | name | varchar(255) | ...

Updating data in a table using identifiers from another table

I would greatly appreciate any assistance in advance! Within my database, I have two tables - var table and val table. var table varid image vartype val table valid sig winner varid These tables are related in a one-to-many relationship, indicating t ...

How can I create a script for a sliding/toggling menu?

Not sure if it's appropriate to ask, but I'm currently in search of a slide/toggle menu. Despite my efforts on Google, I haven't been able to find exactly what I need. As someone who is more skilled in HTML/CSS than jQuery or other scripting ...

What is the process for inserting data into a table by referencing values from another table in SQL?

In my SQL database, I have two tables: albums (album_id, album_name, year) and songs (song_id, song_name, album_name). To link the songs to their respective albums, I added a column called album_id to the songs table using an alter table statement. Now, ...

What is the reason behind having an alternative redux-multi package included in the yarn installation?

After encountering performance issues with npm, I made the switch to the yarn package manager. Everything seemed to be smooth sailing until I noticed a discrepancy with redux-multi when compared to how it functioned with npm. Has anyone else faced this iss ...

Can the browser tabs automatically detect when a user logs out?

When I have multiple tabs open of the same website in a browser, I wonder how other windows can detect when a user has logged out. My development setup involves using Python/Django. The method I am currently implementing is: function user_checking(){ ...