Can a primary key value be a string containing space character(s)?


#1

Is it allowed to have primary key values which are names (AdonisJs = string & MySQL = varchar) containing spaces?

I am asking because each time I do something like this:

const id = 'Primary Key Value With Space Characters'
const foo = await Foo.find(id)

I do not get what I want.

But when id = 'PrimaryKeyValueWithoutSpaceCharacter' all is right.

I do not get my foo instance.


#2

When using Adonis you really want to be using an auto-incrementing integer as your primary key. I would not deviate from this rule under any circumstances.


#3

Thanks but plenty of situations require to have relational database tables with a specific named ID which is not auto increment and which is not integer. I care a lot about DB design so pardon me to disagree with you.


#4

There are indeed; but Adonis is an active record solution modelled on Laravel/Rails and the primary key is a long-standing convention. I would not use Adonis under your circumstances.


#5

If Virk or Romain Lanz confirms what you say (primary key must be an auto increment integer id) , that will be a big problem to me.

I had to change my primary keys nature because of how DB design works and the documentation says it is allowed. But I am facing troubles whenever my primary key is a string containing white space characters.


#6

If you are not using autoincrement, then you need to tell Lucid about it.

class User extends Model {
  static get incrementing () {
    return false
  }
}

#7

Yes, I did that before thanks to the clear documentation you wrote, but my issue is about white spaces in primary key’ values @virk Thank you


#8

What’s the issue you are facing?