-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't work with custom primary keys? #89
Comments
So it is more of a feature than a bug and here's why ORM's need a way to track which model is new and which model already exists in the database. Lucid believes that the primary key will be In your case the primary key is not auto-incrementing and instead set by you. So according to Lucid this row already exists in the database. Action PlanInstead of relying on the |
@thetutlage thank you for the quick feedback! Yeah, I saw you're also using primary key to determine, for example, whether the row has been put to database or not…
What you mean by that? |
A tip: you can use the Lucid Model's insert() method instead of save() to force insertion with custom id-s. I had the same issue and it solved it for me. |
You can try this by installing class Lock extends Lucid {
static get primaryKey () {
return 'bluetooth_id'
}
static get incrementing () {
return false
}
} I'll wait for a day or two to release it to npm. |
@thetutlage yes, this solution works! Thank you! |
The solution works but I have found a related problem. The exists flag may be set upon update too. Now it defaults to false. |
Before updating the model whether you will fetch it from database or you will create a new instance and persist it to database and both of these operations will set the |
when primaryKey is not incrementing, one needs to define it as an explicit flag. Which will aware lucid to setup correct value for primaryKey and make everything work as expected Closes #89
I've just started with Adonis but i've faced a strange issue.
So the docs state that:
Does it mean that I can't create my own primary keys and fill the keys myself?
For example, I have a
Lock
model:Where I want
bluetooth_id
to be the primary key.Then I made a migration:
And a simple blueprint:
All of it is also in the DatabaseSeeder class:
And the result is:
![2017-01-23_14-44-35](https://cloud.githubusercontent.com/assets/2170509/22206184/819c5214-e17a-11e6-85b2-c96111a22811.png)
But apparently, Lucid fails to insert rows when
bluetooth_id
is filled by me (or not autoincremented or has a type ofstring
)Even if it states
seeded database successfully
, actually, zero lines are inserted. Migration does make the correct schema (which I can easily fill using my own SQL-queries) but the table stays empty, no rows are inserted when either seed or just a simplecreate
is called. And no errors produced.Upd: Here is the repo. Feel free to clone and run. You can also
POST /locks
with a json containingbluetooth_id
so see thatLock.create
also failsCould you give me a hand with it?
The text was updated successfully, but these errors were encountered: