views:

56

answers:

1

We're using SchemaExport via ActiveRecord. By default it generates a table like this:

create table List (
  Id      UNIQUEIDENTIFIER   not null,
  Name    NVARCHAR(255)   null,
  OwnerId UNIQUEIDENTIFIER   null,
    primary key ( Id ))

SQL Server then defaults to adding a clustered index for the primary key. But I want this to be nonclustered. I want to add a clustered index to OwnerId as this will be much more efficient.

Now, I could run a script afterwards to create a non-clustered index. This would involve dropping the original primary key constraint and adding a non-clustered one. However, SchemaExport has already helpfully created all my foreign key constraints which stop me dropping the primary key.

So I need to drop the foreign keys, which have an unhelpful name like FK4BAD9607D2BEDDB5, then recreate them (can I do this again automatically?). It's all a bit of a headache.

It would be a lot easier if I could just get in there somehow and add a nonclustered specification to the primary key as it generates it. Is there a relevant bit of the export tool I can override to do this?

Thanks

+1  A: 

I believe your best option is using SchemaExport to create the script, and modify it manually.

Otherwise, you'll need to override Dialect.GetAddPrimaryKeyConstraintString.

Diego Mijelshon
I can't modify it manually unfortunately. The application needs to deploy its own schema, so everything needs to be automated.
Tim
See added option
Diego Mijelshon
Thanks Diego, that's ALMOST the solution. I've added a custom Dialect. The only problem is I have to make every primary key non-clustered, not just on selected tables, because as far as I can see there is no context in the Dialect for what table is being generated.
Tim
Why not have the app generate the script, do search and replace on it, and then execute it?
Diego Mijelshon
In the end, I stuck with the custom dialect.
Tim