Skip to content

Commit fa6e827

Browse files
authored
fix(bigquery): value for datasetID on foreign keys (#8447)
Fixes #8442
1 parent 6b6a69c commit fa6e827

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

bigquery/table.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func bqToForeignKeys(tc *bq.TableConstraints, c *Client) []*ForeignKey {
226226
}
227227
fks = append(fks, &ForeignKey{
228228
Name: fk.Name,
229-
ReferencedTable: c.DatasetInProject(fk.ReferencedTable.DatasetId, fk.ReferencedTable.ProjectId).Table(fk.ReferencedTable.TableId),
229+
ReferencedTable: c.DatasetInProject(fk.ReferencedTable.ProjectId, fk.ReferencedTable.DatasetId).Table(fk.ReferencedTable.TableId),
230230
ColumnReferences: colRefs,
231231
})
232232
}

bigquery/table_test.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"time"
2020

2121
"cloud.google.com/go/internal/testutil"
22+
"github.com/google/go-cmp/cmp"
2223
bq "google.golang.org/api/bigquery/v2"
2324
)
2425

2526
func TestBQToTableMetadata(t *testing.T) {
27+
bqClient := &Client{}
2628
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
2729
aTimeMillis := aTime.UnixNano() / 1e6
2830
aDurationMillis := int64(1800000)
@@ -76,6 +78,22 @@ func TestBQToTableMetadata(t *testing.T) {
7678
PrimaryKey: &bq.TableConstraintsPrimaryKey{
7779
Columns: []string{"id"},
7880
},
81+
ForeignKeys: []*bq.TableConstraintsForeignKeys{
82+
{
83+
Name: "fk",
84+
ColumnReferences: []*bq.TableConstraintsForeignKeysColumnReferences{
85+
{
86+
ReferencedColumn: "id",
87+
ReferencingColumn: "parent",
88+
},
89+
},
90+
ReferencedTable: &bq.TableConstraintsForeignKeysReferencedTable{
91+
DatasetId: "dataset_id",
92+
ProjectId: "project_id",
93+
TableId: "table_id",
94+
},
95+
},
96+
},
7997
},
8098
},
8199
&TableMetadata{
@@ -119,16 +137,32 @@ func TestBQToTableMetadata(t *testing.T) {
119137
PrimaryKey: &PrimaryKey{
120138
Columns: []string{"id"},
121139
},
122-
ForeignKeys: []*ForeignKey{},
140+
ForeignKeys: []*ForeignKey{
141+
{
142+
Name: "fk",
143+
ReferencedTable: &Table{
144+
c: bqClient,
145+
ProjectID: "project_id",
146+
DatasetID: "dataset_id",
147+
TableID: "table_id",
148+
},
149+
ColumnReferences: []*ColumnReference{
150+
{
151+
ReferencedColumn: "id",
152+
ReferencingColumn: "parent",
153+
},
154+
},
155+
},
156+
},
123157
},
124158
},
125159
},
126160
} {
127-
got, err := bqToTableMetadata(test.in, &Client{})
161+
got, err := bqToTableMetadata(test.in, bqClient)
128162
if err != nil {
129163
t.Fatal(err)
130164
}
131-
if diff := testutil.Diff(got, test.want); diff != "" {
165+
if diff := testutil.Diff(got, test.want, cmp.AllowUnexported(Client{}, Table{})); diff != "" {
132166
t.Errorf("%+v:\n, -got, +want:\n%s", test.in, diff)
133167
}
134168
}

0 commit comments

Comments
 (0)