I am getting very weird results when using the Expo’s SQLite module.
Please consider these two tables, i.e., users and messages:
CREATE TABLE IF NOT EXISTS users (
id integer primary key,
email text
);
CREATE TABLE IF NOT EXISTS messages (
id integer primary key,
sender_id integer,
thread_id integer,
body text,
foreign key (sender_id) references users (id),
foreign key (thread_id) references threads (id)
);
Let’s insert some rows into both users and messages:
insert or replace into users (id, email) values (1, 'first@mail.com');
insert or replace into users (id, email) values (2, 'second@mail.com');
insert or replace into users (id, email) values (3, 'third@mail.com');
INSERT INTO messages (id, sender_id, thread_id, body) values (1, 3, 1, 'hello world');
INSERT INTO messages (id, sender_id, thread_id, body) values (2, 1, 1, 'test');
INSERT INTO messages (id, sender_id, thread_id, body) values (3, 2, 1, 'check again');
INSERT INTO messages (id, sender_id, thread_id, body) values (4, 1, 1, 'payload');
Now to get all the messages, including its related user data I need to select from both the tables:
select * from messages inner join users on messages.sender_id=users.id where thread_id =1;
Here the instead of getting results like this (note the id and sender_id:
id sender_id thread_id body id email
---------- ---------- ---------- ----------- ---------- --------------
1 3 1 hello world 3 third@mail.com
2 1 1 test 1 first@mail.com
3 2 1 check again 2 second@mail.co
4 1 1 payload 1 first@mail.com
I am getting results like this:
id sender_id thread_id body id email
---------- ---------- ---------- ----------- ---------- --------------
3 3 1 hello world 3 third@mail.com
1 1 1 test 1 first@mail.com
2 2 1 check again 2 second@mail.co
1 1 1 payload 1 first@mail.com
As you can see from the above results, instead of the message id to have its own value, message’s id is replaced by the sender’s id.
I have made an expo snack demo link here.
What’s going on here?