vitess icon indicating copy to clipboard operation
vitess copied to clipboard

CopySchemaShard should disable FOREIGN_KEY_CHECKS when applying schema

Open bnu0 opened this issue 4 years ago • 3 comments

Problem Description

CopySchemaShard will error out if the schema has any foreign keys, where any table that has a lexicographically lesser name references a table that has a lexicographically greater name.

For example:

create table b (
  id int not null primary key
);

create table a (
  id int not null primary key, 
  b_ref int, 
  foreign key (b_ref) references b (id)
);

When you use CopySchemaShard to copy this to a new keyspace, it will try to create a before b, but error with a SQL error Foreign key constraint is incorrectly formed.

Proposal

Vitess should wrap each CREATE TABLE (or the entire batch) with

SET FOREIGN_KEY_CHECKS = 0;
-- tables created here --
SET FOREIGN_KEY_CHECKS = 1;

Workarounds

You can use GetSchema <tablet-alias> and pipe to jq or similar to pull out the definitions:

 vtctlclient -server x:y GetSchema foo-12345 | jq -r '. table_definitions[] | .schema'

And loop over them, using:

vtctl ExecuteFetchAsDba "SET FOREIGN_KEY_CHECKS = 0; ${SQL}; SET FOREIGN_KEY_CHECKS = 1;"

or similar to copy schema.

bnu0 avatar May 29 '21 11:05 bnu0

Seems reasonable :+1:

deepthi avatar Jun 03 '21 19:06 deepthi

There was an old PR for this: https://github.com/vitessio/vitess/pull/4722

dweitzman avatar Jun 04 '21 07:06 dweitzman

This would be very helpful for us

derekperkins avatar Aug 23 '21 14:08 derekperkins

Closing this as done via https://github.com/vitessio/vitess/pull/15448 as that's where we started disabling FK checks when deploying the schema here: https://github.com/vitessio/vitess/blob/3743f095da6d0063ac9982e9226754bce117b82e/go/vt/vtctl/workflow/materializer.go#L394-L400

mattlord avatar Sep 25 '24 16:09 mattlord