question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UUID is not auto generated

See original GitHub issue

Hi,

I have one issue that would like to ask here.

If I set the id to INTEGER it will be auto-generated, but if I use UUID it’s not. This is my model.

class User extends Model {
  static table = 'users'
  static timestamps = true
  static fields = {
    id: {
      type: DataTypes.UUID,
      primaryKey: true,
    },
    ....
}

Do I miss anything to have this auto-generated?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
eveningkidcommented, Jan 28, 2021

Hi autsada,

For now, UUID fields are not auto-generated indeed.

I just found that Deno has a uuid helper in the standard library, so here’s how you can do it:

import { v4 } from "https://deno.land/std@0.84.0/uuid/mod.ts";

class User extends Model {
  static table = 'users';
  static timestamps = true;
  
  static fields = {
    id: {
      type: DataTypes.UUID,
      primaryKey: true,
    },
  };
  
  static defaults = {
    id: () => v4.generate(),
    // id: v4.generate, should work as well
  };
}

Let me know if this works for you.

Keep in touch

2reactions
royletroncommented, Apr 21, 2021

I feel like I might have won an award for the weirdest bug I have ever see, consider the following user model:

export class User extends Model {
  static table = "users";
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, type: DataTypes.UUID },
    provider: DataTypes.STRING,
    providerUserId: DataTypes.STRING,
    displayName: DataTypes.STRING,
    email: DataTypes.STRING,
  };

  static defaults = {
    id: v4.generate,
  };
}

On running I get a TS error:

Uncaught (in promise) PostgresError: invalid input syntax for type uuid: "function generate() {
    const rnds = crypto.getRandomValues(new Uint8Array(16));
    rnds[6] = (rnds[6] & 0x0f) | 0x40;
    rnds[8] = (rnds[8] & 0x3f) | 0x80;
    return bytesToUuid(rnds);
}"

If I flip the ID type to DataTypes.STRING - it boots up fine, but then when a row is added to the DB the ID is

function generate() {
    const rnds = crypto.getRandomValues(new Uint8Array(16));
    rnds[6] = (rnds[6] & 0x0f) | 0x40;
    rnds[8] = (rnds[8] & 0x3f) | 0x80;
    return bytesToUuid(rnds);
}

It’s evaling the string of the function…?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto-generated primary keys: UUID, serial or identity column?
Numbers generated by a sequence and UUID s are both useful as auto-generated primary keys. Use identity columns unless you need to generate ......
Read more >
UUID not getting auto generated in Spring JPA Hibernate
Simply assign the UUID as the default value: @Entity @Table(name = "decileconfig") public class DecileConfig extends BaseEntity { @Id ...
Read more >
How to generate UUIDs as primary keys with Hibernate
Using Hibernate 6, you can annotate your primary key attribute with @UuidGenerator and set the style to RANDOM, AUTO, or don't specify it....
Read more >
What is a UUID, and why should you care? - Cockroach Labs
(Technically, it's not impossible that the same UUID we generate ... SQL we can ensure it auto-generates a UUID v4 for each row...
Read more >
SQL Primary Key - UUID or Auto Increment Integer / Serial?
Less space. UUID always occupies 16 bytes. For Auto Increment Integer, when stored as Long format, it occupies 8 bytes. If the table...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found