I have a SQL Server database connected to QGIS. I am using SQL Server Management Studio, trying to fill in columns in a new table with values from another table. I know that it is better practice to create a view, but when I attempt to load a view into QGIS, it returns the following error message:

```
Layer is not valid: The layer dbname='MBA-LBA_Ornaments' 
host=DESKTOP-DLA7UV8 estimatedmetadata=true 
srid=4326 type=Point disableInvalidGeometryHandling='1' 
primaryKeyInGeometryColumns='0' table="dbo".
"GoldWeight" (Geo4) is not a valid layer and can not be added to the map.
Reason: No primary key could be found on table GoldWeight

```

The only workaround I can come up with is to create a new table incorporating fields from the Ornaments table and the Sites table. Here is the structure of the relevant tables:

```
CREATE TABLE [dbo].[Ornaments](
    [OrnID] [smallint] IDENTITY(1000,1) NOT NULL,
    [OrnForm] [varchar](20),
    [OrnSubtype] [varchar](20),
    [FragmentYN] [char](1),
    [Material] [varchar](30),
    [Period] [varchar](10),
    [Phase] [varchar](20),
    [BasisOfDate] [varchar](50),
    [SiteID] [smallint],
    [DiscContext] [varchar](20),
    [DiscYear] [varchar](10),
    [SingleFindYN] [char](1),
    [BrokenYN] [varchar](2),
    [DistortedYN] [char](1),
    [WeightG] [numeric](8, 2),
    [SpatialPrecisionRating] [char](1),
    [ImageYN] [char](1),
    [Comments] [varchar](500),

CREATE TABLE [dbo].[Sites](
    [SiteID] [smallint] IDENTITY(1,1) NOT NULL,
    [SiteName] [varchar](50),
    [SiteType] [varchar](50),
    [Region] [varchar](50),
    [CountyDistrict] [nvarchar](30),
    [Latitude] [decimal](11, 6),
    [Longitude] [decimal](11, 6),
    [Comments] [varchar](500),
    [Geo4]  AS (case when [Latitude] IS NOT NULL AND [Longitude] IS NOT NULL 
         then [geography]::Point([Latitude],[Longitude],(4326))  end),

```

The GoldWeight table was created using:

```
SELECT SiteID, SUM(WeightG) AS 'Weight'
INTO GoldWeight
FROM Ornaments
WHERE Material = 'gold'
GROUP BY SiteID

```

I then added columns SiteName and Geo4 using:

```
  ALTER TABLE GoldWeight
        ADD SiteName VARCHAR(50) NULL, 
            Geo4 geography NULL

```

and added a foreign key using:

```
ALTER TABLE GoldWeight
ADD FOREIGN KEY (SiteID) REFERENCES Sites(SiteID)

```

I thought adding a foreign key would populate the fields, but of course that is not true. Is there a way to populate the SiteName and Geo4 fields with the appropriate data based on the SiteID foreign key relationship?