Having difficulty executing a query on an sqlite database

I encountered a syntax error while running a migration script on a sqlite database. Here is the error message I received when executing the migration queries:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: SQLITE_ERROR: near "categoryId": syntax error] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

Below are the SQL queries I am using to create tables:

CREATE TABLE IF NOT EXISTS "Category" (
    id INTEGER PRIMARY KEY NOT NULL,
    name VARCHAR NOT NULL ); 

CREATE TABLE IF NOT EXISTS "Location"(
        id INTEGER PRIMARY KEY NOT NULL,
        name VARCHAR NOT NULL ); 

CREATE TABLE IF NOT EXISTS "Job" (
        id INTEGER PRIMARY KEY NOT NULL,
        createdAt datetime NOT NULL default current_time,
        title VARCHAR NOT NULL,
        description VARCHAR NOT NULL,
        locationId INTEGER NOT NULL,
        FOREIGN KEY(locationId) REFERENCES Location(id),
        categoryId INTEGER NOT NULL,
        FOREIGN KEY(categoryId) REFERENCES Category(id) );

Answer №1

In order to ensure proper data integrity, it is recommended that you define all columns before adding constraints in your table creation script. To do this, simply swap the positions of the 2nd and 3rd last lines as shown below:

CREATE TABLE IF NOT EXISTS "Job" (
        id INTEGER PRIMARY KEY NOT NULL,
        createdAt datetime NOT NULL default current_time,
        title VARCHAR NOT NULL,
        description VARCHAR NOT NULL,
        locationId INTEGER NOT NULL,
        categoryId INTEGER NOT NULL,
        FOREIGN KEY(locationId) REFERENCES Location(id),
        FOREIGN KEY(categoryId) REFERENCES Category(id) );

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

What steps should be taken to fix the sinopia installation error?

While operating on redhat5.9, I encountered an exception related to 'make'. I am curious about what they are trying to create. It seems like it's related to JavaScript. [root@xxxx bin]# npm install -g sinopia --python=/usr/local/clo/ven/pyt ...

When attempting to build in sync-cloud-server, the command "npm install" may lead to encountering errors

Currently, I am in the process of setting up this cloud-sync-server: https://github.com/2-IMMERSE/cloud-sync Upon running npm install, a series of errors pop up: npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a ...

Utilizing various settings using `.env` files in NodeJs

As I work on building a backend in nodejs, one of the key considerations is how to incorporate an environment configuration into the project. I am envisioning a structure where there is a /config folder housing my envparser.ts (still brainstorming a catchi ...

Exploring NodeJS functionality through Mocha testing

Currently in the process of testing an application using Mocha and Chai, I've come across difficulties connecting one of the modules to the test. Here's the test case: "use strict"; var chai = require('chai'); var expect = require("c ...

Error: Running the command 'yarn eg:'live-server'' is not a valid internal or external command

Currently, I am utilizing yarn v1.6.0 to manage dependencies. I have globally installed live-server by running the following command: yarn global-add live-server However, upon executing it as live server, I encounter the following error: 'live-ser ...

Is it advisable to run npm as a superuser?

Having issues with npm errors while attempting to install/update packages without superuser permissions on a Linux system. An alternative solution would be to run sudo npm install <package>, but I am hesitant about whether this is the best approach. ...

I'm all set to launch my express js application! What are the major security concerns that I need to keep in

As a beginner in deploying express applications, I find myself lacking in knowledge about the essential security measures that need to be taken before launching a web application. Here are some key points regarding my website: 1) It is a simple website ...

Major issue still remains unresolved with npx create-react-app

Every time I try to create a new React app with npx create-react-app my-app, I encounter a huge problem. Despite completely deleting Node, npm, and npx twice, re-downloading them, and ensuring that my $PATH is set to usr/local/bin, I am still faced with th ...

The button in my form, created using React, continuously causes the page to refresh

I tried to create a chat application using node.js and react.js. However, I'm facing an issue where clicking the button on my page refreshes the entire page. As a beginner in web development, please forgive me if this is an obvious problem. You can fi ...

Establish a global variable within the utils.js module in a Node.js environment

Hey there, I'm currently in the process of trying to figure out how to properly define a global variable in node.js. I am aware that it's not considered best practice, but in this specific scenario, it seems like the only way to go without involv ...

How does the method of including JavaScript libraries in HTML differ from adding them as npm dependencies?

Upon browsing through npm highly regarded packages, I noticed that popular projects such as Grunt, lodash, and underscore are readily available. I have always utilized these in the traditional manner: <script src="js/lib/lodash.min.js"></script& ...

Unable to post data in Node.js Express

When I try to submit my form from new.js, I encounter the following error: Cannot POST /campgrounds/5b0d6eb8a0f5990b452e8212/comments In the case where I have another Post Route that works properly, it suggests that there are no issues with my body-parse ...

Tips for creating a single-line text in a PDF using Node.js PDF Kit

Currently, I'm utilizing the pdfkit node module for creating a PDF. However, I am facing an issue where I need to place text on a dashed line. Below is the code snippet I am using: doc.moveDown(2) .moveTo(x+leftMargin, doc.y) .lineTo( ...

Structure of a GraphQL project

What is the most effective way to organize a graphQL project on the server side? Here is my current project structure: src config models setup schema queries index userQuery resolvers index userRes ...

Retrieving information from MongoDB

Currently, I am working on retrieving data from MongoDB and passing it to my Express server to eventually display it in my HTML using Angular. The retrieval process is successful when there is only one record in the database. However, if multiple records a ...

Guide on making GET/POST requests with express and react router

I have express set up to serve a static html page where my react components mount. I'm using react router for nested routes, like this: https://i.stack.imgur.com/83bvL.png Within the App component (green outline), I render a Header component (orange ...

Leveraging client API callback variables within a Node.js backend system

If I send a request on the client side using the code snippet below public/foo.js function bar() { fetch('https://api.github.com/') .then(response => response.json()) .then(data => { console.log(data) }) .catch( ...

What is the best method for establishing a connection between NodeJS and PostgreSQL?

I'm having trouble figuring out the correct way to connect a PostgreSQL pool in my NodeJS application. I am using Express with Router, and all of my handlers are located in different files. Many people recommend creating a separate file for the DB con ...

Can I sort the outcomes based on a particular meta key's value?

I am attempting to display the total value of all orders in specific status that have the payment method "ppec-paypal". I have experimented with using the AND function to filter by meta.meta_value. add_shortcode( 'show_total_pp', 'show_tot ...

The Cross-Origin Resource Sharing (CORS) problem doesn't seem to be evident when sending a Postman GET request to Passport.authenticate(), but Nextjs is encountering issues when trying

I've been working on implementing Google Login with a Next.js frontend and an Express.js backend. I followed this tutorial. Using the URL http://localhost:5200/auth/google, I expect to receive the same response as when using Postman: <title>Sig ...