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

CopySchemaShard should disable FOREIGN_KEY_CHECKS when applying schema #8206

Closed
bnu0 opened this issue May 29, 2021 · 4 comments
Closed

CopySchemaShard should disable FOREIGN_KEY_CHECKS when applying schema #8206

bnu0 opened this issue May 29, 2021 · 4 comments
Assignees
Labels
Component: VReplication Type: Enhancement Logical improvement (somewhere between a bug and feature)

Comments

@bnu0
Copy link
Contributor

bnu0 commented May 29, 2021

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.

@deepthi deepthi added Component: VReplication Type: Enhancement Logical improvement (somewhere between a bug and feature) labels Jun 3, 2021
@deepthi
Copy link
Member

deepthi commented Jun 3, 2021

Seems reasonable 👍

@dweitzman
Copy link
Member

There was an old PR for this: #4722

@derekperkins
Copy link
Member

This would be very helpful for us

@mattlord
Copy link
Contributor

Closing this as done via #15448 as that's where we started disabling FK checks when deploying the schema here:

_, err = mz.tmc.ApplySchema(mz.ctx, targetTablet.Tablet, &tmutils.SchemaChange{
SQL: sql,
Force: false,
AllowReplication: true,
SQLMode: vreplication.SQLMode,
DisableForeignKeyChecks: true,
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: VReplication Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants