Search code examples
knex.js

How to use a absolute file path as binding parameter?


I try to use a absolute file path as binding parameters. But it throw an syntax error:

exports.up = async (knex: Knex): Promise<any> => {
  const dest2 =
    '/Users/ldu020/workspace/github.com/mrdulin/nodejs-pg-knex-samples/src/knex-migration/using-a-remove-csv-file/migrations/geotargets-2019-05-02.csv';

  await knex.raw(
    `
      copy t1_geotargets(criteria_id, criteria_name, canonical_name, parent_id, country_code, target_type, status)
      from ?? DELIMITER ',' CSV;
    `,
    [dest2]
  );
};

Here is the error message:

migration file "20190515121901_import-a-remote-csv-file.ts" failed
migration failed with error: 
  copy t1_geotargets(criteria_id, criteria_name, canonical_name, parent_id, country_code, target_type, status)
  from "/Users/ldu020/workspace/github"."com/mrdulin/nodejs-pg-knex-samples/src/knex-migration/using-a-remove-csv-file/migrations/geotargets-2019-05-02"."csv" DELIMITER ',' CSV;
   - syntax error at or near ""/Users/ldu020/workspace/github""

How can I solve this?

update

I think I find the issue. The reason is the . symbol. But don't know how to process it.


Solution

  • That is not possible, because in identifier binding periods are interpreted as separators for table and column names.

    You have to enter that manually like knex.raw(`... from "${dest2}" DELIMITER ...`)