Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound Primary Key

Tags:

f#

So this is pretty straight forward. I can't seem to find an answer for this on the web.

In Fluent Migrator, I see the option to tag a column with PrimaryKey() but I don't see anything for creating compound primary keys. It doesn't look like there's an overload for PrimaryKey either.

Is this possible?

base.Create.Table(tableName).WithColumn("Id").AsGuid().PrimaryKey().NotNullable().Unique()
                .WithColumn("c1").AsGuid().NotNullable().Unique()
                .WithColumn("c2").AsString().NotNullable()
                .WithColumn("c3").AsString().NotNullable()
                .WithColumn("c4").AsDateTime()
                |> ignore

In this example I'd like to use c2 and c3 as a composite primary key

like image 366
Anthony Russell Avatar asked Aug 21 '17 12:08

Anthony Russell


2 Answers

In FluentMigrator, you can create a composite primary key like this:

Create.PrimaryKey("PK_Table").OnTable("Table").WithSchema("schemaname")
    .Columns([|"Col1"; "Col2"|])
    |> ignore
like image 104
Chad Gilbert Avatar answered Nov 12 '22 11:11

Chad Gilbert


I seem to remember you could just apply the PrimaryKey() method to multiple columns. So to use c2 and c3 as a composite primary key for example:

base.Create.Table(tableName)
    .WithColumn("Id").AsGuid().NotNullable().Unique()
    .WithColumn("c1").AsGuid().NotNullable().Unique()
    .WithColumn("c2").AsString().NotNullable().PrimaryKey()
    .WithColumn("c3").AsString().NotNullable().PrimaryKey()
    .WithColumn("c4").AsDateTime()
    |> ignore
like image 3
Caltor Avatar answered Nov 12 '22 13:11

Caltor