sapiens/SqlFu

Table with primary key of type Guid

Closed this issue · 1 comments

I written a simple console application for test the SqlFu and I found a isssue when the primary key data type is a guid.

I have the following User class and Metadata:

public partial class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string MailAddress { get; set; }
    public string Job { get; set; }
}

[Table("Users")]
[Index("MailAddress", Name = "idx_mailaddress")]
[PrimaryKey("Id", Name = "PK_Users", AutoIncrement = false)]
public class UserMetadata
{
    [ColumnOptions(Size = "75")]
    public string MailAddress { get; set; }

    [ColumnOptions(IsNullable = true, Size = "100")]
    public string Job { get; set; }
}

[MetadataType(typeof(UserMetadata))]
public partial class User
{

}

And when SqlFu build the script to create the User table, the result is:

create table [Users] (
[Id] uniqueidentifier NOT NULL IDENTITY(1,1),
[Name] nvarchar(max) NOT NULL,
[MailAddress] nvarchar(75) NOT NULL,
[Job] nvarchar(100) NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY ([Id]));
CREATE INDEX [idx_mailaddress] ON [Users] ([MailAddress]);
go

The SqlFul put the text "IDENTITY(1,1)" even when I set AutoIncrement = false.

I forked the source code for debug and I did a simple adjust in the source code that worked well for SQL Server provider (I have not tested in others providers).

I push the adjust in a branch of my repository that is available in:

master...alanlima:guid_as_primarykey_for_sql_server

(I can do a pull request??)

It a simple adjust, but I think is better add a new attribute in the class ColumnDefinition (maybe AutoIncrement) to indicate that the column is auto increment. What do you think?

OK, I've created a PR and merged it into the devel branch. Thank you for your contribution.

About adding a new option for the ColumnDefinition, I don't know yet, I have to think about it.