How to auto generate UUID/GUID with Fluent Migrator in MySQL?
Asked Answered
D

1

8

I'm trying to set a default value NewGuid() to a UUID/GUID column in MySQL 5.7 (with FluentMigrator in .Net Core).

Create.Table("table")
.WithColumn("auto_guid").AsGuid().NotNullable().WithDefaultValue(SystemMethods.NewGuid)

Based on Fluent Migrator documentation NewGuid should be supported but I get an error:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ..."

I searched for any similiar issues and solutions but couldn't find any.

Is auto generating UUID/GUID in MySQL even possible with Fluent Migrator?

Duclos answered 16/9, 2019 at 17:17 Comment(1)
I would say, this depends on your mysql version. Before v8.0.13, all solutions here on SO use trigger to generate guid as default value. From v8.0.13 mysql supports certain functions as default values for fields.Fennell
I
4

You are using the wrong method. Use .WithDefault(SystemMethods.NewGuid) instead of .WithDefaultValue().

Integer answered 9/12, 2019 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.