Psql error: column "paraglider.idMaker" does not exist but it exist - javascript

Hello i got a query that throw the this error : error: column "paraglider.idMaker" does not exist
const result = await client.query('SELECT *, "maker.is" AS "maker_id", "maker.name" AS "maker_name" FROM "paraglider" JOIN "maker" ON "paraglider.idMaker"="maker.id"');
There no error of typo
This work perfect in the CLI PSQL:
SELECT paraglider.*,
maker.id AS maker_id,
maker.name AS maker_name
FROM paraglider
JOIN maker ON "idMaker"=maker.id;
I got this result:
id | name | type | release_year | created_at | idMaker | maker_id | maker_name
----+--------+----------+--------------+---------------+---------+----------+------------
1 |Tonic 2 | all | 2019 | time_stamp_ex | 1 | 1 | Skywalk
2 |Arcus RS| all | 2017 | time_stamp_ex | 2 | 2 | Swing
I don't understand why my query work in the CLI of PSQL but not in my code.
I try to change:
'ON "paraglider.idMaker"="maker.id"'
in
'ON "idMaker"="maker.id"'
and the error change in err: error: column "maker.id" does not exist.

Ok i find a query who work :
SELECT paraglider.*, maker.id AS maker_id, maker.name AS maker_name FROM paraglider JOIN maker ON "idMaker"=maker.id
But don't understand where the problem come from...

Related

Dexiejs - Filtering returns single value

My dexiedb structure looks like below.
const db = new Dexie('shipment-execution');
db.version(1).stores({
root: '++id,shipmentid,stopid',
})
My IndexedDB will look like below
| id| shipmentid| stopid|
| 1 | 6000001 | 10 |
| 2 | 6000001 | 20 |
| 3 | 6000001 | 30 |
| 4 | 6000002 | 10 |
| 5 | 6000002 | 20 |
I am trying to get all the data which has shipmentid equal to 6000001. Each time it is returning only one record (the first record ).
db.root.where({shipmentid : '6000001'}).toArray().then((d)=>{
console.log(d);
})
output is only fetching the first record
| id| shipmentid| stopid|
| 1| 6000001 | 10 |
Please let me know, what I am doing wrong.

Sequelize: how to do nested subquery in one to many field?

I have two tables; one is "user" another is "post". User has a one-to-many relationship with "post". "post" has a foreign key field named user_id.
user table:
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
| id | uuid | name | first_name | last_name | email | password | role | createdAt | updatedAt |
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
| 1 | 37b4e543-ae46-45ef-a216-dea4ee79d6f7 | riyad | 0 | 1 | riyad#gmail.com | 123456 | 1 | 2021-11-22 17:56:56 | 2021-11-22 17:56:56 |
| 2 | d63c3414-db52-4784-9de8-b99a6659f1c9 | riyad2 | 6 | 7 | riyad2#gmail.com | 123456 | 5 | 2021-11-22 20:01:36 | 2021-11-22 20:01:36 |
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
post table:
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
| id | title | body | user_id | slug | createdAt | updatedAt |
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
| 1 | title 1 | body 1 | 1 | 6e95b4a2-f101-48cd-8fea-8b53850c54b0 | 2021-11-22 17:57:56 | 2021-11-22 17:57:56 |
| 2 | title 2 | body 2 | 1 | 47d0cdbe-af3b-446a-badf-5197d8796133 | 2021-11-22 17:58:02 | 2021-11-22 17:58:02 |
| 3 | title 3 | body 3 | 2 | 347e200b-bffd-423d-b481-48d1b74ecc75 | 2021-11-22 20:02:04 | 2021-11-22 20:02:04 |
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
Now I want to get the users who posted from 2021-11-22 15:00:00 to 2021-11-22 20:00:00.
In this case the first two posts (post id 1 and 2) were made in this time which were posted by user_id 1. Now I want to get that user(s) details in single query.
This is the SQL query I would do like use:
select *
from user
where id = any(select user_id from post
where createdAt >= "2021-11-22 15:00:00"
and created_at <= "2021-11-22 20:00:00")
Now how can I write that in sequelize?
You want this Sequelize doc page.
Long story short (at least in my opinion), sub queries will be hard to handle in any ORM. I would advise to use raw SQL instead.

Graphs and adjacency list practice

Hey guys so I am currently work on a problem involving graphs and adjacency lists but it is not working. This is for self-study only and it is not for school or homework.
I am given this table of values:
| Start name | End name | Example path found by a search |
|------------|----------|-----------------------------------|
| carrie | carrie | carrie (it's the same node) |
| carrie | humza | carrie -> humza |
| carrie | yervand | carrie -> jun -> silla -> yervand |
| ophelia | ursula | ophelia -> travis -> **FAIL** |
| travis | carrie | travis -> ophelia -> **FAIL** |
| ursula | ophelia | ursula -> travis -> ophelia |
| victor | humza | victor -> **FAIL** |
But my code is just not working it is saying that I am not getting this:
from carrie to carrie
from carrie to humza
And I am doing it with breath-first as recommended by them and Idk what the issue is
if (!adjacencyList[startName]) return null;
const queue = [startName];
let visited = new Set();
let currentVertex;
if (adjacencyList[startName] === adjacencyList[endName]) return adjacencyList[startName]
while(queue.length) {
currentVertex = queue.shift();
if (!visited.has(currentVertex)) {
visited.add(currentVertex);
adjacencyList[currentVertex].forEach(neighbor => queue.push(neighbor))
}
}
if (visited.has(endName)) {
return visited;
}
else {
return null
}
}
Idk what the issue is and how to fix it. I thought that by returning the set I would return the path but apparently not.

how to use a where clause to see if an id exists in a postgres array type column

What I'm trying to do is, join an "events" table on two other tables: "users" and "locations".
Every "event" happens at a specific time and on a specific "location" which needs joining in order for the user to know where the event will take place. And is also created by a specific "user". this user happens to have an array of user ids called close_friends. The three tables are the following:
Events table
===================================================
Column | Type |
-----------------------+--------------------------+
id | integer |
location_id | integer |
guests | integer |
hide_from | integer[] |
only_close_friends | boolean |
is_public | boolean |
min_viable_population | integer |
max_viable_population | integer |
created_by | integer |
sport_type | text |
whats_needed | text[] |
happens_at | timestamp with time zone |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
updated_by | integer |
___________________________________________________
Users table
===========================================
Column | Type |
---------------+--------------------------+
id | integer |
username | text |
password_hash | text |
first_name | text |
last_name | text |
gender | text |
photo | text |
city | text |
bio | text |
height | integer |
sports | text[] |
photos | text[] |
friends | integer[] |
close_friends | integer[] |
scopes | text[] |
verified | boolean |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
updated_by | integer |
___________________________________________
Locations table
==========================================
Column | Type |
---------------+--------------------------+
id | integer |
name | text |
maps_url | text |
city | text |
region | text |
cost | text |
photo | text |
meta | text |
sport_types | text[] |
girls_allowed | boolean |
verified | boolean |
created_at | timestamp with time zone |
created_by | integer |
updated_at | timestamp with time zone |
updated_by | integer |
Now The user should be able to create an event which is visible only to his/her close friends.
what I wanna do is fetch only the events that are to be seen by creator's close friends. To give you an overall idea of what I'm trying to achieve, I'd provide you with the code block that looks good but returns an "Operator doesn't exist" error.
const query = db('events')
.join('users', 'users.id', '=', 'events.created_by')
.join('locations', 'locations.id', '=', 'events.location_id')
.select(
'events.id',
'events.location_id',
'locations.name',
'events.guests',
'locations.city',
'events.min_viable_population',
'events.max_viable_population',
'events.created_by',
'events.sport_type',
'events.whats_needed',
'events.happens_at',
'events.created_at',
'users.username',
'users.first_name',
'users.last_name',
'users.photo',
'users.verified'
)
.where({'only_close_friends': true})
.whereIn('close_friends', function () { /////////////////////////
this.select('id') /////////////////////////
.from('users') // The problem is here //
.where({ id: user_id }) /////////////////////////
}) /////////////////////////
Note
"user_id" is the id of the correctly logged in user.
I was able to find a simple solution with the help of a friend:
.whereRaw('? = ANY(users.close_friends)',[user_id])

MySQL select all values which share this field's value

I am new to MySQL so describing problems in words is difficult and searching for solutions is extremely challenging.
This problem is best explained visually:
I want to select (as an array) exchange_pair_id's that share the same pair_id.
So in the above data, my MySQL query would return an object:
{ pair_id: 1, exchange_pair_id: [183, 1] }
I am aware this is a butchered question, but I do not know the words to search to solve this problem.
Updated for clarity/brevity:
+------------------+-------------+---------+
| exchange_pair_id | exchange_id | pair_id |
+------------------+-------------+---------+
| 1 | 3 | 1 |
+------------------+-------------+---------+
| 183 | 1 | 1 |
+------------------+-------------+---------+
| 69 | 2 | 2 |
+------------------+-------------+---------+
| 12 | 4 | 2 |
+------------------+-------------+---------+
| 2 | 3 | 2 |
+------------------+-------------+---------+
| 3 | 3 | 3 |
+------------------+-------------+---------+
Desired output from a Javascript MySQL select query:
[
{ pair_id: 1, exchange_pair_id: [1, 183] },
{ pair_id: 2, exchange_pair_id: [69, 12, 2] },
{ pair_id: 3, exchange_pair_id: [3] }
]
I am thinking a query like this , but I'm waiting your answer at comments.
Basically, you use GROUP BY to obtain in two different columns the values for each pair_id:
SELECT pair_id, MIN(exhange_pair_id) AS id1, MAX(exchange_pair_id) AS id2
FROM yourtable
GROUP BY pair_id;
Update version: Can you try this please on your data?
In this case MYSQL let you concat field using a separator (,)
SELECT pair_id, GROUP_CONCAT(exhange_pair_id) AS exhange_pair_id
FROM yourtable
GROUP BY pair_id
try select exchange_pair_id from yourtable where pair_id=1 then it will return array [1,183]
The SQL you are looking for would be:
SELECT * WHERE pair_id == 1
Without knowing what your specific code looks like, I can only guess as to how you are implementing a call to your database. I would assume you are doing some sort of async call to a PHP controller. So instead of using '1' in the query, you will need to use whatever variable you are pulling from your code to know what you are looking for.

Categories

Resources