sequelize making foreign keys composite and unique index. - node.js

Here is my model ,
var traders = sequelize.define('traders', {
.....
}, {});
it has many to many self association
traders.belongsToMany(models.traders,{
as:'feedbackClient',
through:'feedback'
});
idea is one trader can give feedback to other trader on each successful trade.
but when i sync it generates table with this SQL query
Executing (default): CREATE TABLE IF NOT EXISTS "feedbacks" ("id" S`ERIAL , "rating" "public"."enum_feedbacks_rating", "comment" VARCHAR(255), "traderId" INTEGER REFERENCES "traders" ("id") ON DELETE CASCADE ON UPDATE CASCADE, "feedbackClientId" INTEGER REFERENCES "traders" ("id") ON DELETE CASCADE ON UPDATE CASCADE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, UNIQUE ("traderId", "feedbackClientId"), PRIMARY KEY ("id"));`
how can i remove this constraint?
UNIQUE ("traderId", "feedbackClientId")
so that i can add multiple records with same combination of traderId and feedbackClientId.

Got a solution here, please post your answers if you have better solutions.

Related

How to insert new rows to a junction table Postgres

I have a many to many relationship set up with with services and service_categories. Each has a table, and there is a third table to handle to relationship (junction table) called service_service_categories. I have created them like this:
CREATE TABLE services(
service_id SERIAL,
name VARCHAR(255),
summary VARCHAR(255),
profileImage VARCHAR(255),
userAgeGroup VARCHAR(255),
userType TEXT,
additionalNeeds TEXT[],
experience TEXT,
location POINT,
price NUMERIC,
PRIMARY KEY (id),
UNIQUE (name)
);
CREATE TABLE service_categories(
service_category_id SERIAL,
name TEXT,
description VARCHAR(255),
PRIMARY KEY (id),
UNIQUE (name)
);
CREATE TABLE service_service_categories(
service_id INT NOT NULL,
service_category_id INT NOT NULL,
PRIMARY KEY (service_id, service_category_id),
FOREIGN KEY (service_id) REFERENCES services(service_id) ON UPDATE CASCADE,
FOREIGN KEY (service_category_id) REFERENCES service_categories(service_category_id) ON UPDATE CASCADE
);
Now, in my application I would like to add a service_category to a service from a select list for example, at the same time as I create or update a service. In my node js I have this post route set up:
// Create a service
router.post('/', async( req, res) => {
try {
console.log(req.body);
const { name, summary } = req.body;
const newService = await pool.query(
'INSERT INTO services(name,summary) VALUES($1,$2) RETURNING *',
[name, summary]
);
res.json(newService);
} catch (err) {
console.log(err.message);
}
})
How should I change this code to also add a row to the service_service_categories table, when the new service ahas not been created yet, so has no serial number created?
If any one could talk me through the approach for this I would be grateful.
Thanks.
You can do this in the database by adding a trigger to the services table to insert a row into the service_service_categories that fires on row insert. The "NEW" keyword in the trigger function represents the row that was just inserted, so you can access the serial ID value.
https://www.postgresqltutorial.com/postgresql-triggers/
Something like this:
CREATE TRIGGER insert_new_service_trigger
AFTER INSERT
ON services
FOR EACH ROW
EXECUTE PROCEDURE insert_new_service();
Then your trigger function looks something like this (noting that the trigger function needs to be created before the trigger itself):
CREATE OR REPLACE FUNCTION insert_new_service()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
-- check to see if service_id has been created
IF NEW.service_id NOT IN (SELECT service_id FROM service_service_categories) THEN
INSERT INTO service_service_categories(service_id)
VALUES(NEW.service_id);
END IF;
RETURN NEW;
END;
$$;
However in your example data structure, it doesn't seem like there's a good way to link the service_categories.service_category_id serial value to this new row - you may need to change it a bit to accommodate
I managed to get it working to a point with multiple inserts and changing the schema a bit on services table. In the service table I added a column: category_id INT:
ALTER TABLE services
ADD COLUMN category_id INT;
Then in my node query I did this and it worked:
const newService = await pool.query(
`
with ins1 AS
(
INSERT INTO services (name,summary,category_id)
VALUES ($1,$2,$3) RETURNING service_id, category_id
),
ins2 AS
(
INSERT INTO service_service_categories (service_id,service_category_id) SELECT service_id, category_id FROM ins1
)
select * from ins1
`,
[name, summary, category_id]
);
Ideally I want to have multiple categories so the category_id column on service table, would become category_ids INT[]. and it would be an array of ids.
How would I put the second insert into a foreach (interger in the array), so it creates a new service_service_categories row for each id in the array?

How to structure nested arrays with postgresql

I'm making a simple multiplayer game using postgres as a database (and node as the BE if that helps). I made the table users which contains all of the user accounts, and a table equipped, which contains all of the equipped items a user has. users has a one -> many relationship with equipped.
I'm running into the situation where I need the data from both tables structured like so:
[
{
user_id: 1,
user_data...
equipped: [
{ user_id: 1, item_data... },
...
],
},
{
user_id: 2,
user_data...
equipped: [
{ user_id: 2, item_data... },
...
],
},
]
Is there a way to get this data in a single query? Is it a good idea to get it in a single query?
EDIT: Here's my schemas
CREATE TABLE IF NOT EXISTS users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW(),
last_login TIMESTAMP,
authenticated BOOLEAN NOT NULL DEFAULT FALSE,
reset_password_hash UUID
);
CREATE TABLE IF NOT EXISTS equipment (
equipment_id SERIAL PRIMARY KEY NOT NULL,
inventory_id INTEGER NOT NULL REFERENCES inventory (inventory_id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users (user_id) ON DELETE CASCADE,
slot equipment_slot NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT only_one_item_per_slot UNIQUE (user_id, slot)
);
Okay so what I was looking for was closer to postgresql json aggregate, but I didn't know what to look for.
Based on my very limited SQL experience, the "classic" way to handle this would just to do a simple JOIN query on the database like so:
SELECT users.username, equipment.slot, equipment.inventory_id
FROM users
LEFT JOIN equipment ON users.user_id = equipment.user_id;
This is nice and simple, but I would need to merge these tables in my server before sending them off.
Thankfully postgres lets you aggregate rows into a JSON array, which is exactly what I needed (thanks #j-spratt). My final* query looks like:
SELECT users.username,
json_agg(json_build_object('slot', equipment.slot, 'inventory_id', equipment.inventory_id))
FROM users
LEFT JOIN equipment ON users.user_id = equipment.user_id
GROUP BY users.username;
Which returns in exactly the format I was looking for.

Sequelize (with Postgres) - Define a new model with engine = postgres

Scenario: I am dealing with a database which is running both postgres and infobright, where infobright is set to default engine.
Goal: Have Sequelize create a table in postgres
Problem: Sequelize is creating table in Infobright by default.
Detailed Explanation:
So when I am trying to define a model which look like:
sequelize.define('mymodel', {
file_name: {
type: Sequelize.STRING,
}
}, {
schema: 'myschema'
});
The sql that get generated looks like:
create
table
if not exists "myschema"."mymodel"(
"id" serial,
"file_name" varchar(255),
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone not null,
primary key("id")
);
NOTE: Because with (engine = postgres) is missing, the system tries to create table in the default engine i.e. infobright, which is not what I expect it to do.
Expected SQL
create
table
if not exists "myschema"."mymodel"(
"id" serial,
"file_name" varchar(255),
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone not null,
primary key("id")
) with (
engine = postgres
);
Note: Executing the above generates a table in postgres.
I tried specifying engine in sequelize as well as model configuration, but it doesn't work.
Questions: How shall I configure Sequelize or my model so that it generates the expected sql. Open to other suggestions as well.

better-sqlite3 SqliteError: NOT NULL constraint failed

I'm trying to get my username and password hash into my SQLite database, but I always get the error message: SqliteError: NOT NULL constraint failed: Users.id
This error indicates that my query is not matching my Users table, but I can't find the issue.
My dump for Users table:
CREATE TABLE IF NOT EXISTS "Users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"username" varchar(50) NOT NULL,
"hash" varchar(60) NOT NULL
);
My Node code:
var Database = require('better-sqlite3')
var db = new Database('db.sqlite')
var sql = 'INSERT INTO Users (username, hash) VALUES (?,?)'
db.prepare(sql).run(username, hash)
The id field is set to be an autoincrementing integer and the primary key, therefore it should set itself automatically for every new entry.
The error message says that the NOT NULL contraint on Users.id failed, but id doesn't even have the NOT NULL constraint defined.
If I instead use the command
INSERT INTO Users (username, hash) VALUES ('testname','testhash1234');
directly in the SQLite3 database it all works fine.
Am I doing something wrong or is this a bug in better-sqlite3?
How can I get around this problem?

Delete related records , having has_many relationship

i have has_many relationship between two tables
like : "questions"-< "options" [ a question have many options]
my class structure is :
for question
class Model_Admin_sysQuestion extends ORM {
protected $_table_name = 'questions';
protected $_has_many = array(
'options' => array(
'model' => 'Admin_sysQuestionOption',
'foreign_key' => 'question_id',
),
);... .
for options
Class Model_Admin_sysQuestionOption extends ORM {
protected $_table_name = 'questions_options';
protected $_belongs_to = array(
'question' => array(
'model' => 'Admin_sysSection',
'foreign_key' => 'question_id',
),
); .... .
and i m trying to delete questions with all its options with the following code:
$question = ORM::factory('Admin_sysQuestion', 30);
$question->options->delete($question->id);
$question->delete();
but it is giving error
error":"Cannot delete admin_sysquestionoption model because it is not loaded."
any idea? how to do it?
When loading multiple relations you must call find_all:
foreach($question->options->find_all() as $option)
{
$option->delete();
}
Or use DB QBuilder for multiple deleting:
DB::delete('questions_options')
->where('question_id', '=', 30)
->execute($this->_db);
In addition to the post of #biakevoron you also have the MySQL option; just add a relationship between the two tables with an on delete cascade requirement on the options table. Personally, I also check for objects/rows relational to the current object working on, covering both the explicit (delete relational objects) as the implicit side of the game. You should in fact be able to trust MySQL doing it's job (which it does pretty well), but if someone ie. alters the relationship, or switches the table to MyIsam you'll probably not notice untill the table get's really, really big.
Play around with the following table. It forces a strict belongs-to relationship on the options table, meaning an options can only exist if it's parent exists.
CREATE TABLE `options` (
`id` int(10) unsigned NOT NULL auto_increment,
`question_id` int(10) unsigned NOT NULL,
`option` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE `questions` (
`id` int(10) unsigned NOT NULL auto_increment,
`question` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `options`
ADD CONSTRAINT `options_belongs_to` FOREIGN KEY (`id`) REFERENCES `questions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Note - The only restriction is you need to put the tables on the InnoDB engine (which is slightly different from the default MyIsam engine, checkout a summary here and a more extensive version here.

Resources