Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
Are nullable foreign keys possible with objectionjs/knex?

Maybe this is a simple fix, but I can't seem to figure out what I'm doing wrong here.

I have a table that lists all the states Model:

static get jsonSchema() {
    return {
      type: 'object',

      properties: {
        id: { type: 'integer' },
        name: { type: 'string', minLength: 1, maxLength: 100 },
      },
    }
  }

  static get relationMappings() {
    return {
      users: {
        relation: Model.HasManyRelation,
        modelClass: User,
        join: {
          from: `${tableNames.state}.id`,
          to: `${tableNames.user}.state_id`,
        },
      },
    }

Migration:

await knex.schema.createTable(tableNames.state, (table) => {
    table.increments().primary().notNullable()
    table.string('name', 100).notNullable()

User table model:

static get jsonSchema() {
    return {
      type: 'object',

      properties: {
        id: { type: 'integer' },
        first_name: { type: 'string', minLength: 1, maxLength: 100 },
        last_name: { type: 'string', minLength: 1, maxLength: 100 },
        state_id: { type: 'integer', default: null },
      },
    }
  }

  static get relationMappings() {
    return {
      state: {
        relation: Model.BelongsToOneRelation,
        modelClass: State,
        join: {
          from: `${tableNames.user}.state_id`,
          to: `${tableNames.state}.id`,
        },
      }
    }
  }

User table migration:

await knex.schema
    .createTable(tableNames.user, (table) => {
      table.increments().primary().notNullable()
      table.string('first_name', 100).notNullable()
      table.string('last_name', 100).notNullable()
      table.integer('state_id').unsigned()

      table
        .foreign('state_id')
        .references('id')
        .inTable(tableNames.state)
        .onDelete('SET NULL')
    })

Now the issue: I want the state_id column to be nullable, as in not every user will have a state assigned to them. But when I try inserting a user with no state_id, I get this: insert or update on table \"user\" violates foreign key constraint \"user_state_id_foreign\".

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

two things you are doing wrong

  1. in your json schema define your column as state_id: {type: ['integer', 'null']}
  2. in your user migrations make table.integer('state_id').unsigned().nullable()
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda