Sequelize error: An unknown column is encountered in the field list while including a model without specifying the `attributes` field

Utilizing Sequelize for executing MySQL queries in my codebase, I have meticulously defined Models and connected them with their associations.

Creating a music playlist structure with:

  • Artists who can have multiple songs.
  • Playlists that contain multiple songs as well.
  • Songs belonging to one or multiple playlists and may have one or multiple associated artists.

The models are structured as follows:

I have created an interface within each model containing the necessary fields for type safety and convenience.

ARTIST

export interface IArtist {
    ID_ARTIST: number,
    NAME: string
}

export const Artist = seq.define<Model<IArtist>>("Artist", {
    ID_ARTIST: {
        primaryKey: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true
    },
    NAME: DataTypes.STRING(30)
}, {
    timestamps: true,
    createdAt: true,
    updatedAt: false,
    tableName: "ARTISTS"
})

PLAYLIST

export interface IPlaylist {
    ID_PLAYLIST: number,
    NAME: string
}

export const PlayList = seq.define<Model<IPlaylist>>("Playlist", {
    ID_PLAYLIST: {
        primaryKey: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
    },
    NAME: DataTypes.STRING(20)
}, {
    timestamps: true,
    createdAt: true,
    updatedAt: false,
    tableName: "PLAYLISTS"
})

SONG

export interface ISong {
    ID_SONG: number,
    ID_ARTIST: number,
    ID_PLAYLIST: number,
    DURATION: number,
    NAME: string
}

export const Song = seq.define<Model<ISong>>("Song", {
    ID_SONG: {
        primaryKey: true,
        type:</questionbody>
<exquestionbody>
<div class="question">
                
<p>Im trying to use Sequelize for MySQL queries, in my code I defined my Models and links them with his respective associations.</p>
<p>Trying to simulate a music playlist that have:
Artists, they can have multiples songs.</p>
<p>Playlist, has multiples songs too.</p>
<p>Songs, they are part of 1 or multiple playlists and have 1 or multiple artists.</p>
<p>That models are defines like this:</p>
<blockquote>
<p>In each model I defined a interface with his respective fields for safe type and comfort</p>
</blockquote>
<p>ARTIST</p>
<pre><code>export interface IArtist {
    ID_ARTIST: number,
    NAME: string
}

export const Artist = seq.define<Model<IArtist>>("Artist", {
    ID_ARTIST: {
        primaryKey: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true
    },
    NAME: DataTypes.STRING(30)
}, {
    timestamps: true,
    createdAt: true,
    updatedAt: false,
    tableName: "ARTISTS",
})

PLAYLIST

export interface IPlaylist {
    ID_PLAYLIST: number,
    NAME: string
}

export const PlayList = seq.define<Model<IPlaylist>>("Playlist", {
    ID_PLAYLIST: {
        primaryKey: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
    },
    NAME: DataTypes.STRING(20)
}, {
    timestamps: true,
    createdAt: true,
    updatedAt: false,
    tableName: "PLAYLISTS"
})

SONG

export interface ISong {
    ID_SONG: number,
    ID_ARTIST: number,
    ID_PLAYLIST: number,
    DURATION: number,
    NAME: string
}

export const Song = seq.define<Model<ISong>>("Song", {
    ID_SONG: {
        primaryKey: true,
        type:</exquestionbody>
<answer1>
<div class="answer" i="77628315" l="4.0" c="1702046406" a="QW5hdG9seQ==" ai="1376618">
<p>You just need to define paired associations with the same <code>foreignKey option, see my answer here

Answer №1

To establish paired associations with the identical foreignKey option, refer to my solution outlined in this post

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

Is it possible to establish multiple connections simultaneously using Stomp?

I have been utilizing the ng2-stomp-service in my Angular application. Is it advisable to set up multiple connections (not just multiple subscriptions)? In the past, I have always seen a single connection being established, with several subscriptions made ...

Using PHP script to retrieve MySQL table values and dynamically update a live graph in a Javascript function

I've been researching similar issues for quite some time now, but to no avail. Currently, I'm utilizing Highcharts to update a graph every 3 seconds with the latest entry from a specific MySQL table. I am referring to the example Javascript code ...

Transform a list of objects into an array

I am currently working on a project that requires me to generate questions randomly in order to create a question paper. To achieve this, I have created a class called Gene to represent a question. I then created a list called QuestionList of type Gene, wh ...

React Native is facing difficulty in fetching pagination data which is causing rendering errors

Currently, I am working on fetching pagination data from an API. The process involves retrieving data from https://myapi/?page=1, then from https://myapi/?page=2, and so on. However, despite following this logic, I encountered an error that has left me puz ...

jQuery animation for expanding accordion headers

I want to create a cool animation for my accordion headers that mimics a ribbon being dragged onto the wrapper when hovered over, and then dragged out when the hover ends. Everything was working fine in this initial jsFiddle, but as soon as I tried to ani ...

Passing data from a parent node to a child node using JavaScript and the D3.js library

Creating a legend (var legend) for my d3js chart involves binding data to the parent 'g' element, specifically a string (label) that is needed in the child 'text' element. Any ideas on how to assign the parent data from the 'g&apo ...

Can I use Mongoose to apply a filter to this request?

I have a requirement to query all products with the category status set to true. My product model looks like this: const ProductSchema = Schema({ name : {type: String}, category: {type: Schema.Types.ObjectId, ref: 'Category'} }) Normally, I wou ...

Creating a script to identify and separate odd and even numbers between 1 and 1000 using Javascript

I have been working through a book on Javascript that includes solved examples, but I've come across one example without a solution. I am interested in learning how to complete it. The task at hand is to write a script in Javascript (for a browser) t ...

Attempting to create a simple echo function that has the added capability of querying a MySQL table

I've been struggling to pinpoint the source of the error in my code. My MySQL query writing skills are not the best, and I'm unsure if that's the root cause of the issue affecting the entire function. Here's the PHP code snippet: ...

Changing VueJS duplicate values with v-model (:value, @input)

I'm encountering an issue with v-model in my custom component. I prefer not to use State or Bus. Currently, the component successfully returns a single value in App.js, but it duplicates itself. I'm struggling to resolve this problem, so any help ...

Scroll up and down to witness the enchanting interplay of fading in and out

Looking to expand upon the solution given in this response. The existing code effectively fades in an element while scrolling down and fades out an image when scrolling up; however, it lacks the functionality to fade out when scrolling down. I am aiming ...

Potential Unresolved Promise Rejection (ID: 0): The object 'prevComponentInstance._currentElement' is undefined

Attempting to fetch JSON data in react native using axios.get(my_url_path), then updating the state with response.data under the key 'urldatabase'. When attempting to access this state key and read the data from the JSON, an error is encountered: ...

SQL command to calculate the number of entries in a longtext column

Consider the following table structure: profile_data : id(int), age(int), gender(varchar), goals(longtext) I am looking to create a query that calculates the average number of goals set by each unique ID. The challenge here is that the 'goals' ...

Ways to make the sole element you've designed fade into view

I have a function that creates a note in the DOM. The issue is that when the note is created, it adds the "fade in" animation class to all notes instead of just the one being created. How can I modify it so that only the newly created note will have the ...

Changing the data type of an integer to a string within an Object

I'm working with two arrays of objects. I need to convert the data values in the first format from integers to strings, but I'm struggling to find a simple solution. Is there anyone who can provide assistance? https://i.sstatic.net/mzqWE.png If ...

Integrate Angular 2 into the current layout of Express framework

After generating an express structure with express-generator, I ended up with the standard setup: bin bld node_modules public routes views app.js package.json Now, I want to enhance the views and routes directories by organizing them as follows: v ...

Leveraging the "dialogclose" event within jQuery Mobile

Is there a way to change an image once it is clicked and change back when the dialog it links to is closed? I found a potential solution on this site, but after modifying it for my needs, it doesn't seem to work. Can anyone help me figure out what I a ...

Issue with Context Menu Not Triggering on Dynamically Added Elements in JQuery

Check out the JSFiddle Demo Within my email sidebar, I implemented a custom right-click feature that allows users to add new sub-folders. The code snippet below demonstrates how this functionality works: if ($(this).hasClass('NewSubFolder')) { ...

Troubleshooting: Issue with AngularJS ng-click functionality within Modal

My Modal doesn't open when the button inside it is clicked. The ng-click function in the js file is not being called without any error messages. However, outside of the Modal, the ng-click works fine. <div class="container-fluid" ng-app="app" ng- ...

Material UI offers a feature that allows for the grouping and auto-completion sorting

I am currently utilizing Material UI Autocomplete along with React to create a grouped autocomplete dropdown feature. Here is the dataset: let top100Films = [ { title: "The Shawshank Redemption", genre: "thriller" }, { title: " ...