Search code examples
node.jspostgresqlexpressknex.js

Migrating a table in knex: storing media, image or a blob


I am trying to migrate a relation to my postgres database. The problem is I have no clue what value type to use for an image.

exports.up = function (knex, Promise) => {
  return knex.schema.createTable('observations', (table) => {
    table.increments();
    table.integer('user_id').notNullable();
    table.blob('image').notNullable(); //???
    table.string('category').notNullable();
    table.string('description').notNullable();
    table.boolean('approved').notNullable().defaultTo(false);
    table.float('latitude').notNullable();
    table.float('longitude').notNullable();
    table.timestamp('created_at').defaultTo(knex.fn.now());
  });
};

I thought there would be a 'blob' file type but in the documentation there seems to be no sign of migrating any media.

Please help me.


Solution

  • Looks like table.binary should fit the bill.

    The PostgreSQL data type should be bytea.