【问题标题】:Table's id is replaced by the foreign key's id表的 id 被外键的 id 替换
【发布时间】:2018-03-11 01:10:35
【问题描述】:

我在我的 react native 项目中使用 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 是发件人的 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),
            );
        }
    )
}

【问题讨论】:

  • 有趣。我试图在小吃中复制代码,我得到了相同的结果。这是expo link
  • @Kakar 是的!而已。不知道这里出了什么问题。

标签: react-native sqlite expo


【解决方案1】:

用解决方案编辑:

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

这有效:select messages.*, users.first_name, users.last_name, users.email from messages inner join users on messages.sender_id=users.id where thread_id = ?;

【讨论】:

  • 对不起,但结果相同。
  • 那我们需要看一些代码,我不认为你在这里发布的内容有什么问题
  • 嗨!我已经用相关代码更新了这个问题。请你看一下。
  • 看不出有什么问题。你能做一个最小的例子来复制snack.expo.io上的问题吗?
  • 有趣。我试图在小吃中复制代码,我得到了相同的结果。这是expo link
猜你喜欢
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
相关资源
最近更新 更多