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

Cannot update with timestamps set to true #113

Closed
lpc921 opened this issue May 11, 2022 · 1 comment
Closed

Cannot update with timestamps set to true #113

lpc921 opened this issue May 11, 2022 · 1 comment
Labels
type: bug Something isn't working as intended or expected.

Comments

@lpc921
Copy link

lpc921 commented May 11, 2022

description

Using the update method on a model with primary or unique field, and timestamps: true option results in exception.

steps to reproduce

  1. Create a model with timestamps option to true
  2. Update an item to the table using .update

source

const { connect } = require("trilogy");

async function test() {
  const db = connect(":memory:");

  const model = await db.model(
    "tests",
    {
      id: { type: Number, primary: true },
      item: String,
    },
    { timestamps: true }
  );
  await model.create({ id: 1, item: "test1" });
  await model.update({ id: 1 }, { item: "test2" });
}

test();

error

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: update `tests` set `item` = 'test2' where `id` = 1 - SQLITE_ERROR: no such column: current_timestamp] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

expected

Update should work

actual

Exception was thrown

@lpc921 lpc921 added the type: bug Something isn't working as intended or expected. label May 11, 2022
@haltcase
Copy link
Owner

Hey @lpc921, nice find! This was a bug in the timestamp update trigger that escaped current_timestamp as if it were a column.

 const TimestampTriggerTemplate = `
 create trigger if not exists :name: after update on :modelName: begin
-  update :modelName: set :column: = \`current_timestamp\`
+  update :modelName: set :column: = current_timestamp
   where :key: = \`old\`.:key:;
 end
 `

I've pushed a patch already but it looks like there are some dependency issues to work out before a release can happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working as intended or expected.
Projects
None yet
Development

No branches or pull requests

2 participants