Replies: 8 comments 13 replies
-
Hey @mohux! 👋 There's no |
Beta Was this translation helpful? Give feedback.
-
This post worked for me https://forum.adonisjs.com/t/how-to-use-uuid-as-primary-key-postgre/3918 |
Beta Was this translation helpful? Give feedback.
-
I figured out that the problem is of how Lucid treats primaryKey via @column({isPrimary:true}) |
Beta Was this translation helpful? Give feedback.
-
Did anyone find a solution? i tried it with PostgreSQL, its working so perfectly, without the need to use consume function |
Beta Was this translation helpful? Give feedback.
-
I'm also using Mysql(MariaDb), and I use the beforeCreate hook in order to add an uuid: import { v4 as uuidv4 } from "uuid";
export default class User extends BaseModel {
@beforeCreate()
public static async addUidHook(user: User) {
user.uid = uuidv4();
}
...
@column({ isPrimary: true, columnName: "uid" })
public uid: string;
}
|
Beta Was this translation helpful? Give feedback.
-
If you use Postgres, there's a clean DRY way to add UUID primary keys with auto-generated IDs (no 1. Enable the UUID extension (migration must precede any models using UUIDs)import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
public up() {
this.schema.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
}
public down() {
this.schema.raw('DROP EXTENSION IF EXISTS "uuid-ossp"')
}
} 2. Create migrations for your modelsimport BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
protected tableName = 'my_model'
public async up() {
this.schema.createTable(this.tableName, (table) => {
// use Postgres builtin function 'uuid_generate_v4' to auto-generate an id
table.uuid('id').primary().defaultTo(this.raw('uuid_generate_v4()'))
// more columns…
})
}
// …
} 3. Define your model classexport default class MyModel extends BaseModel {
@column({ isPrimary: true })
public id: string
// ...
} Refer to primary uuid columns in related models: export default class extends BaseSchema {
public async up() {
this.schema.createTable('my_related_model', (table) => {
// …
table.uuid('my_model_id').references('id').inTable('my_model').onDelete('CASCADE')
// …
})
}
} |
Beta Was this translation helpful? Give feedback.
-
Here my solution, since previews code didn't worked for me on adonis 6. Insert uuid from model export default class User extends compose(BaseModel, AuthFinder) {
static selfAssignPrimaryKey = true
@column({ isPrimary: true })
declare id: string
@beforeCreate()
static async setId(user: User) {
user.id = crypto.randomUUID()
}
}
this.schema.createTable(this.tableName, (table) => {
table.uuid('id').primary().defaultTo(crypto.randomUUID())
}) |
Beta Was this translation helpful? Give feedback.
-
Another solution using preload:
This code will run for every model. |
Beta Was this translation helpful? Give feedback.
-
I'm using AdonisJS v5 and not v4
when i use User.create()
and add @beforeCreate hook to create a uuid
it returns 0 instead of the stored uuid
therefore the solution of adding
static async get increments(){}
does not work
I tried so many things to fix the issue
but it always return 0
Beta Was this translation helpful? Give feedback.
All reactions