Skip to content
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

How to let app insert id automatically? #811

Closed
ri8ika opened this issue Feb 13, 2019 · 20 comments
Closed

How to let app insert id automatically? #811

ri8ika opened this issue Feb 13, 2019 · 20 comments
Assignees

Comments

@ri8ika
Copy link

ri8ika commented Feb 13, 2019

In loopback 3, the id was inserted automatically. But in loopback 4, it is required even when creating models.

@property({
  type: 'string',
  id: true,
  required: true
})
id?: string;

After removing the required: true option worked when I was working with mongodb - the id was created automatically when post request happens. But for giving it a try to live on heroku, I needed to use postgres rather than mongodb. So, I decided to create new project locally and using postgresql the preceeding way didn't work.

Meaning removing required: true doesn't work. Even having required: false doesn't work.

500 error: null value in column "id" violates not-null constraint

Is there any way to have id not required when inserting data so that it can insert automatically?

PS: If I remove the id property the app will not work: Property 'id' does not exist. I hope there should be a way.

@dhmlau
Copy link
Member

dhmlau commented Feb 13, 2019

@ri8ika, IIRC, last time I got it to work for postgresql is:

  @property({
    type: 'string',
    id: true,
    generated: true,
  })

In case you're running issue with the postgresql setup, I've put together some information here: https://medium.com/loopback/loopback-4-todo-list-example-switching-to-a-sql-or-nosql-database-24cd68cf3647.

@dhmlau dhmlau self-assigned this Feb 13, 2019
@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

Ah, I tried it but still getting error:

500 error: null value in column "id" violates not-null constraint

@dhmlau
Copy link
Member

dhmlau commented Feb 14, 2019

@ri8ika , I just tried it out.

@property({
    type: 'number',
    id: true,
    generated: true,
  })
  id: number;

Please note that the id variable (last line of the above snippet) is not optional.

@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

Sorry, still not working for me. Maybe the type is string in my case?

@property({
    type: 'string',
    id: true,
    generated: true,
  })
  id: string;

@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

I just tried using number, but still getting same error. Are you also using postgres?

@dhmlau
Copy link
Member

dhmlau commented Feb 14, 2019

Yes, I'm using postgres. I assume you're not using the automigrate function to create the table?
I just realized that if I use npm run migrate to get the table generated, it has some extra values in the property/column that we want to have id auto-gen.

testdb=# \d customer
                               Table "public.customer"
  Column  |  Type   | Collation | Nullable |                 Default                  
----------+---------+-----------+----------+------------------------------------------
 custid   | integer |           | not null | nextval('customer_custid_seq'::regclass)
 custname | text    |           | not null | 

See the nextval....

I'm hitting a bug described in here: loopbackio/loopback-next#2398.
Still investigating.

@dhmlau
Copy link
Member

dhmlau commented Feb 14, 2019

@ri8ika, I think I might know the key to get this to work. I created the table on postgresql using npm run migrate command. In that case, it adds the nextval in the column as mentioned above.

I've tried it with column type being integer or text (on postgresql). You can see my repo:
https://github.com/dhmlau/loopback4-example-todo-customized (see models\customer.model.ts and the CustomerController to test).

Assuming you created the table outside of LoopBack, did you set up the auto-increment for that column? I'm reading this.. http://www.postgresqltutorial.com/postgresql-serial/

@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

Hmm, I had run the migration, but currently not seeing such nextval

@dhmlau
Copy link
Member

dhmlau commented Feb 14, 2019

did you run npm run build to make sure the latest code got transpiled? just in case..

@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

Never. But why to run build? I'm testing it in development. Still no luck running after build.

@ri8ika
Copy link
Author

ri8ika commented Feb 14, 2019

I will try to figure out on this later. But I'm facing a major issue. Can you please help me on this?

https://github.com/strongloop/loopback.io/issues/813

The repo I have hosted in heroku is:

github.com/bhojport/nexserver

@dhmlau
Copy link
Member

dhmlau commented Feb 16, 2019

@ri8ika , i just had a chance to review your repo, and noticed that the id field is optional. https://github.com/bhojport/nexserver/blob/master/src/models/product.model.ts#L43-L48.

It should be the following instead:

  @property({
    type: 'string',
    id: true,
    generated: true
  })
  id: string; <---- HERE

You can see my example here.

@ri8ika
Copy link
Author

ri8ika commented Feb 16, 2019

@dhmlau Yes, it's optional there. I have not updated the repo. But as per your suggestion, I already tried and didn't get it working - locally and so not updated in repo.

@dhmlau
Copy link
Member

dhmlau commented Feb 21, 2019

@ri8ika , I haven't got a chance to run your example. But if it's the same as my example, try running npm run clean on your app and see if it helps.

@ri8ika
Copy link
Author

ri8ika commented Feb 21, 2019

Sorry, I can't migrate the db in heroku so stopped working on it.

@warrenlalata
Copy link

loopback has the worst docs

@ctrevisan
Copy link

Just ran into a similar issue. As @dhmlau pointed out, the generated property needs to be set to true. However, auto-updating (npm run migrate) doesn't seem to set the column to auto-increment; only auto-migrating (npm run migrate -- --rebuild, which drops the tables and creates them again) seemed to work.

@emredmrl
Copy link

@ctrevisan thank you!!!

@kumawatajay215
Copy link

This remove table data, this is not good solution if you do not want to loss data

@kumawatajay215
Copy link

kumawatajay215 commented Apr 6, 2023

@ctrevisan If you are using Postgres db then you can use query to set default value to primary
CREATE SEQUENCE "table_id_seq" INCREMENT 1 START 1; alter table public."table" alter column id set default nextval('"table_id_seq"'::regclass)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants