Skip to content

sql: can't create table, index and foreign key in the same transaction #24626

@ruzkant

Description

@ruzkant

This is on cockroachdb 2.0.0 on linux 64 bit.

These statements:

start transaction;
create table t_parent (id varchar primary key);
create table t_child (id varchar primary key, parent_id varchar);
create index idx_t_child_parent_id on t_child (parent_id);
alter table t_child add constraint fk_t_child_parent_id foreign key (parent_id) references t_parent (id);
commit;

Give this error:

pq: foreign key requires an existing index on columns ("parent_id")

I am not sure if this is an issue or a known limitation. I am working on a java application which uses automatic migration from the create scripts, but they fail when used in a transaction. I hope to avoid having only the foreign keys moved to their own transaction and separating the index code from its corresponding alter code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sql-pgcompatSemantic compatibility with PostgreSQLC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)O-communityOriginated from the communitydocs-known-limitationdocs-todo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions