Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a multi-tenant scenario, assume you have 2 tables: Subscriber &amp; Contact.</p> <p>You want to know at a glance who is the Primary Contact for the Subscriber. However, the Contact table must also have a FKEY reference to SubscriberId for partitioning / to be used for federation key etc (SQL Azure).</p> <pre><code>======================================================================= Subscriber.sql ======================================================================= -- One who has subscribed to Rhipheus CREATE TABLE [rhipheus].[Subscriber] ( [Id] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() CONSTRAINT [PKEY_Subscriber_Id] PRIMARY KEY CLUSTERED, [ShortName] NVARCHAR(50) NOT NULL, [LegalName] NVARCHAR(255) NOT NULL, [SmallLogoPath] NVARCHAR(MAX) NOT NULL, [LargeLogoPath] NVARCHAR(MAX) NOT NULL, [PrimaryContactId] UNIQUEIDENTIFIER NULL REFERENCES [rhipheus].[Contact]([Id]), ) ==================================================================== Contact.sql ==================================================================== CREATE TABLE [rhipheus].[Contact] ( [Id] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() CONSTRAINT [PKEY_Contact_Id] PRIMARY KEY CLUSTERED, [SubscriberId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [FKEY_Contact_SubscriberId_Subscriber_Id] REFERENCES [rhipheus].[Subscriber]([Id]), [FirstName] NVARCHAR(50) NOT NULL, [LastName] NVARCHAR(50) NOT NULL, ) </code></pre> <p>This used to work 2010 Database project because it used to strip away all column level constraints and create them using separate ALTER scripts.</p> <p>The way I resolved this in VS.Net 2012 is by declaring the foreign key column as NULLable and adding foreign keys on Subscriber using a separate ALTER statement. Of course, SQL Server project in VS 2012 wouldn't allow me to do it column-level the declaration as it is not able to figure out which table to create first (even when the HINT is right there in the form of NULLable declaration).</p> <pre><code>======================================================================= Subscriber.sql ======================================================================= -- One who has subscribed to Rhipheus CREATE TABLE [rhipheus].[Subscriber] ( [Id] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() CONSTRAINT [PKEY_Subscriber_Id] PRIMARY KEY CLUSTERED, [ShortName] NVARCHAR(50) NOT NULL, [LegalName] NVARCHAR(255) NOT NULL, [SmallLogoPath] NVARCHAR(MAX) NOT NULL, [LargeLogoPath] NVARCHAR(MAX) NOT NULL, [PrimaryContactId] UNIQUEIDENTIFIER NULL ) ==================================================================== Contact.sql ==================================================================== CREATE TABLE [rhipheus].[Contact] ( [Id] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() CONSTRAINT [PKEY_Contact_Id] PRIMARY KEY CLUSTERED, [SubscriberId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [FKEY_Contact_SubscriberId_Subscriber_Id] REFERENCES [rhipheus].[Subscriber]([Id]), [FirstName] NVARCHAR(50) NOT NULL, [LastName] NVARCHAR(50) NOT NULL ) ==================================================================== Subscriber.ForeignKeys.sql ==================================================================== ALTER TABLE [rhipheus].[Subscriber] ADD CONSTRAINT [FKEY_Subscriber_PrimaryContactId_Contact_Id] FOREIGN KEY([PrimaryContactId]) REFERENCES [rhipheus].[Contact]([Id]) GO </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload