Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
¿Cómo generar consultas sql sin procesar a partir de migraciones knex?

Pregunta

Por alguna razón, no puedo usar las migraciones knex en el entorno de producción.

¿Puedo generar SQL sin procesar a partir de migraciones de knex como operaciones de selección/inserción/actualización de knex?

Por ejemplo:

Puedo obtener SQL sin procesar de oraciones seleccionadas con:

 Knex('t_example').select('id').toQuery() // => select id from t_example

Pero no sé cómo generar SQL sin formato a partir de los archivos de migración:

 import { Knex } from 'knex' // how to get raw SQL like // "alter table `t_example` add `foo` int"; // from the "up" function export async function up (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.integer('foo') }) } export async function down (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.dropColumn('foo') }) }

Mis intentos:

importe el archivo de migración e invoque la función up (de esteproblema de github )

algo como

 const knex = require('knex')({...}); const {up, down} = require('./migrations/20200304140624_createUsersTable.js'); console.log(up(knex).toSQL().toNative()); console.log(down(knex).toSQL().toNative());

El fragmento de código anterior funciona bien para algunas migraciones (createTable en mi caso), mientras que no funciona para otras (addColumn).

Por ejemplo, con este archivo de migración:

 import { Knex } from 'knex' export async function up (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.integer('foo') }) } export async function down (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.dropColumn('foo') }) }

Lanza un mensaje de error: TypeError: up(...).toSql is not a function

y

UnhandledPromiseRejectionWarning: Error: ER_DUP_FIELDNAME: Duplicate column name 'foo'

ejecutar la migración con el indicador DEBUG

En primer lugar, deshaga las migraciones con knex migrate:rollback

Luego, ejecute las migraciones con el indicador DEBUG:

 DEBUG=knex:query knex migrate:latest

Produce registros SQL como

 knex:query alter table `t_example` add `bar` int trx2 +8ms`

Puedo obtener SQL de estas salidas ahora, pero requiere algunas tareas de formato adicionales.

¿Hay una solución mejor?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!