Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 表的id被外键的id替换_React Native_Sqlite_Expo - Fatal编程技术网

React native 表的id被外键的id替换

React native 表的id被外键的id替换,react-native,sqlite,expo,React Native,Sqlite,Expo,我在react原生项目中使用Expo的SQLite模块。我有两个表、消息和用户: CREATE TABLE IF NOT EXISTS messages ( id text not null, sender_id integer, thread_id integer, body text, foreign key (sender_id) references users (id), foreign key (thread_id) reference

我在react原生项目中使用Expo的SQLite模块。我有两个表、消息和用户:

CREATE TABLE IF NOT EXISTS messages (
    id text not null,
    sender_id integer,
    thread_id integer,
    body text,
    foreign key (sender_id) references users (id),
    foreign key (thread_id) references threads (id)
);

CREATE TABLE IF NOT EXISTS users (
    id integer primary key,
    first_name text,
    last_name text,
    email text
);
如果我插入一些信息:

INSERT INTO messages (id, sender_id, thread_id, body) values ('xeieoef-ee, 3, 1, 'test');
INSERT INTO messages (id, sender_id, thread_id, body) values ('ttrefzr-ry, 3, 1, 'payload');
我希望通过比较线程id来获取所有消息,包括其用户的数据。这就是我的疑问:

select * from messages, users where thread_id = 2 AND messages.sender_id = users.id;
但是,这会导致消息id和用户id相同:

[
    {
      "body": "test",
      "email": "userthree@gmail.com",
      "first_name": "userThreeF",
      "id": 3,
      "last_name": "threeUserL",
      "sender_id": 3,
      "thread_id": 1,
    },
    {
      "body": "payload",
      "email": "userthree@gmail.com",
      "first_name": "userThreeF",
      "id": 3,
      "last_name": "threeUserL",
      "sender_id": 3,
      "thread_id": 1,
    },
]
邮件id不是要有自己的id,而是发件人的id。我在这里做错了什么

更新代码

我在应用程序中有一个按钮,onpress会将数据发送到MessageStore的功能

sendText = () => {
    const {MessageStore, UserStore, SocketStore} = this.props;
    const data = {
        id: uuid.v4(),
        sender_id: UserStore.userInfo.user.id,
        thread_id: 1,
        body: this.state.text,
    }
    MessageStore.addMessageToDB(data);
}
在存储中,addMessageToDB将消息添加到数据库中,它再次调用getMessageFromDatabase来获取所有消息

@action addMessageToDB = (payload) => {
    db.transaction(
        tx => {
            tx.executeSql(
                `INSERT INTO messages
                    (id, sender_id, thread_id, body, status) values (?, ?, ?, ?, ?);`,
                [payload.id, payload.sender_id, payload.thread_id, payload.body, "pending"],
                (tx, results) => {
                    console.log('message insertion success')
                },
                (tx, error) => console.log('message insertion error', error)
            );
            this.getMessageFromDatabase(payload.thread_id);
        }
    )
}

@action getMessageFromDatabase = (payload) => {
    console.log('>>> getMessageFromDatabase payload', payload);
    db.transaction(
        tx => {
            tx.executeSql(
                `select * from messages inner join users on messages.sender_id=users.id where thread_id = ?;`, [payload],
                (tx, {rows}) => {
                    console.log('inner join success', rows._array);
                },
                (tx, error) => console.log('inner join error', error),
            );
        }
    )
}
使用解决方案编辑:

两个表都有一个“id”列,因此用户id将覆盖消息id


这是有效的:选择messages.*,users.first\u name,users.last\u name,users.email from messages internal join users on messages.sender\u id=users.id其中thread\u id=

很抱歉,但这有相同的结果。我们需要查看一些代码,我不认为你在这里发布的内容有任何问题嗨!我已经用相关代码更新了问题。你能看一下吗?看不出有什么问题。你能举一个简单的例子在snack.expo.io上复制这个问题吗?很有趣。我尝试在snack中复制代码,得到了相同的结果。有趣的是。我尝试在snack中复制代码,得到了相同的结果。这是@Kakar是的!就这样。不知道这里怎么了。