Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support FOREIGN KEY without CONSTRAINTS clause #103

Merged
merged 1 commit into from
May 30, 2024

Conversation

nktks
Copy link
Contributor

@nktks nktks commented May 29, 2024

What

and table_constraint is:
    [ CONSTRAINT constraint_name ]
    { CHECK ( expression ) |
      FOREIGN KEY ( column_name [, ... ] ) REFERENCES  ref_table  ( ref_column [, ... ] )
        [ ON DELETE { CASCADE | NO ACTION } ]

Background

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1a6ec22]

goroutine 33 [running]:
testing.tRunner.func1.2({0x1d85d80, 0x305f670})
	/opt/hosted-tool-cache/go/1.21.10/x64/src/testing/testing.go:1545 +0x3f7
testing.tRunner.func1()
	/opt/hosted-tool-cache/go/1.21.10/x64/src/testing/testing.go:1548 +0x716
panic({0x1d85d80?, 0x305f670?})
	/opt/hosted-tool-cache/go/1.21.10/x64/src/runtime/panic.go:920 +0x270
github.com/gcpug/handy-spanner/server.(*database).registerInformationSchemaTables(0xc00069c500, {0x2308c80, 0x40d35e0}, 0xc000998180)
	/path/to/vendor/github.com/gcpug/handy-spanner/server/database.go:1290 +0x6e2
...
  • When we use FOREIGN KEY without CONSTRAINTS clause, native spanner seems to insert autogenerated CONSTRAINT_NAME to INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS.
> spanner-cli -v
Connected.
spanner> CREATE TABLE Child
      -> (
      ->     ChildID STRING(MAX) NOT NULL,
      ->     ParentID STRING(MAX) NOT NULL,
      ->     FOREIGN KEY (ParentID) REFERENCES Parent (ParentID),
      -> ) PRIMARY KEY(ChildID);
Query OK, 0 rows affected (5.78 sec)

spanner> SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS;
+--------------------+-------------------+------------------------------------+---------------------------+--------------------------+------------------------+--------------+-------------+-------------+---------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME                    | UNIQUE_CONSTRAINT_CATALOG | UNIQUE_CONSTRAINT_SCHEMA | UNIQUE_CONSTRAINT_NAME | MATCH_OPTION | UPDATE_RULE | DELETE_RULE | SPANNER_STATE |
+--------------------+-------------------+------------------------------------+---------------------------+--------------------------+------------------------+--------------+-------------+-------------+---------------+
|                    |                   | FK_Child_Parent_EDF8147E7630B0BA_1 |                           |                          | PK_Parent              | SIMPLE       | NO ACTION   | NO ACTION   | COMMITTED     |
+--------------------+-------------------+------------------------------------+---------------------------+--------------------------+------------------------+--------------+-------------+-------------+---------------+
  • I'm not sure what value EDF8147E7630B0BA is, so for this case I insert fmt.Sprintf("FK_%s_%s_%d", tableName, cnst.ReferenceTable.Name, i) instead.

@sinmetal sinmetal self-requested a review May 30, 2024 03:11
@double-di
Copy link
Collaborator

LGTM

@double-di double-di merged commit 6807aff into gcpug:master May 30, 2024
1 check passed
@nktks nktks deleted the fix/panic-fk-column branch May 30, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants