objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Unable to define PascalCase column names for relations

Open rmp135 opened this issue 9 years ago • 5 comments

Not sure if I've missed something in the docs but I'm having trouble setting the to and from tables for relationships to use PascalCase column names e.g. Game.CardID, it just refuses to enter the column into the insert statement. I've managed to get around this by using ~~camelCase~~ snake_case but it took hours to figure out. You might want to make it more obvious in the documentation that this format is required.

rmp135 avatar Nov 11 '17 22:11 rmp135

If you don't have any case conversion set up for your models you need to give every column in the database format. If your columns are pascal cased in the db, then use pascal case in relation mappings. If they are snake cased, then use snake case. Objection doesn't require you to use any specific casing. You have probably messed something else up. If you post your migrations and models here I may be able to help.

koskimas avatar Nov 11 '17 23:11 koskimas

I don't have case conversion setup.

My models couldn't really be simpler.

export default class CardModel extends Model {
  static get tableName() {
    return 'Card'
  }
  static get relationMappings() {
    return {
      game: {
        relation: Model.BelongsToOneRelation,
        modelClass: GameModel,
        join: {
          from: 'Card.GameID',
          to: 'Game.ID'
        }
      }
    }
  }
}
export default class GameModel extends Model {
  static get tableName() {
    return 'Game'
  }
  static get relationMappings() {
    return {
      cards: {
        relation: Model.HasManyRelation,
        modelClass: CardModel,
        join: {
          from: 'Game.ID',
          to: 'Card.GameID'
        }
      }
    }
  }
}

The database columns are in pascal case. If I keep the relations in pascal case and attempt to perform a graph insert

GameModel.query().graphInsert({
  cards: [{ Text: 'card a' }, { Text: 'card b' }]
})

The GameID columns of the cards are skipped, thus failing the insert. If I change the case of the relation to camel case then they do insert but performing a select with eager('cards') fails (it was performing two queries with a 'where 1 = ?' on the cards, very strange). The only remedy was to change both database and relation names to snake case.

rmp135 avatar Nov 11 '17 23:11 rmp135

The default id column is lowercase id. You probably need to add this to your models:

static get idColumn() {
  return 'ID';
}

koskimas avatar Nov 11 '17 23:11 koskimas

Was that it?

koskimas avatar Nov 12 '17 00:11 koskimas

Looks like that fixed it. Thanks.

rmp135 avatar Nov 12 '17 09:11 rmp135