Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update 有 bug #42

Open
musicode opened this issue Nov 30, 2017 · 6 comments
Open

update 有 bug #42

musicode opened this issue Nov 30, 2017 · 6 comments

Comments

@musicode
Copy link

proto.update = function* (table, row, options) {
  // TODO: support multi rows
  options = options || {};
  if (!options.columns) {
    options.columns = Object.keys(row);
  }
  if (!options.where) {
    if (!('id' in row)) {
      throw new Error('Can not auto detect update condition, please set options.where, or make sure obj.id exists');
    }
    options.where = {
      id: row.id,
    };
  }

  const sets = [];
  const values = [];
  for (let i = 0; i < options.columns.length; i++) {
    const column = options.columns[i];
    if (column in options.where) {
      continue;
    }
    sets.push('?? = ?');
    values.push(column);
    values.push(row[column]);
  }
  const sql = this.format('UPDATE ?? SET ', [ table ]) +
    this.format(sets.join(', '), values) +
    this._where(options.where);
  debug('update(%j, %j, %j) \n=> %j', table, row, options, sql);
  return yield this.query(sql);
};

用例

update(
  'table',
  {
    status: 1,
  },
  {
    status: 0
  }
)

把所有未读的 status 改成已读,这里执行下来,sets 是个 []

@musicode
Copy link
Author

musicode commented Dec 4, 2017

这个库是不是不维护了。。。

@wondger
Copy link

wondger commented Dec 27, 2017

@musicode

update(
'table',
{
status: 1,
},
{
where: {status: 0}
}
)

@musicode
Copy link
Author

if (!options.columns) {
    options.columns = Object.keys(row);
  }
for (let i = 0; i < options.columns.length; i++) {
    const column = options.columns[i];
    if (column in options.where) {
      continue;
    }

这两句过滤了,所以没用

@sophister
Copy link

嗯,我也遇到这个问题了,这种 update 的字段同时在 where 里的,目前都是自己拼的SQL……不太明白,库里为什么要加这个限制

@GuoJiKaiHehe
Copy link

我也感觉了,更改的时候,会出现错乱

@JerrysShan
Copy link

这个库是不是不维护了,这样的bug都不修复了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants