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

During schema generation, ignore duplicate column references. #91

Conversation

vnayar
Copy link
Contributor

@vnayar vnayar commented May 10, 2024

When creating @manytoone relationships, the column is typically located Currently, during schema generation, a check is put in place to avoid attempting to create duplicate columns. For example:

@Entity class Pet {
  @Id string name;
  @ManyToOne @JoinColumn("person_fk") Person owner; // OK
  // @ManyToOne @JoinColumn("person_fk") Person owner2; // ERROR - Duplicate column
}

@Entity class Person {
  @Id string name;
}

The code above represents the tables, which allow many pets to reference the same person.

CREATE TABLE pet (name varchar, person_fk varchar);
CREATE TABLE person (name varchar);

However, there are new ways to have a @manytomany relationship when using an @EmbeddedId. Consider RoboPets, which have a name, and also a manufacturer. Many RoboPets can share the same manufacturer, there's a many-to-one relationship.

@Embeddable class PetPk {
  string name;
  string manufacturerId;
}

@Entity class RoboPet {
  @EmbeddedId PetPk pk;
  // This works on existing schemas, but fails while generating schemas with the error:
  // "duplicate column name robot_pet.manufacturer_id in schema"
  @ManyToOne @JoinColumn("manufacturer_id") Manufacturer manufacturerId;
}

@Entity class Manufacturer {
  @Id string manufacturerId;
}

The code above represents the tables:

CREATE TABLE robo_pet(name varchar, manufacturer_id varchar);
CREATE TABLE manufacturer(manufacturer_id);

The many-to-one relationship is established because many RoboPets share the same "manufacturer_id" column. This relationship is currently flagged as an error, because the column mentioned in @joincolumn on RoboPet.manufacturerId is already defined, however, it is a valid relationship and functions on schemas that have already been created.

When creating @manytoone relationships, the column is typically located  Currently, during schema generation, a check is put in place to avoid attempting to create
duplicate columns. For example:

```
@entity class Pet {
  @id string name;
  @manytoone @joincolumn("person_fk") Person owner; // OK
  // @manytoone @joincolumn("person_fk") Person owner2; // ERROR - Duplicate column
}

@entity class Person {
  @id string name;
}
```

The code above represents the tables, which allow many pets to reference the same person.
```
CREATE TABLE pet (name varchar, person_fk varchar);
CREATE TABLE person (name varchar);
```

However, there are new ways to have a @manytomany relationship when using an @EmbeddedId.
Consider RoboPets, which have a name, and also a manufacturer. Many RoboPets can share the
same manufacturer, there's a many-to-one relationship.

```
@embeddable class PetPk {
  string name;
  string manufacturerId;
}

@entity class RoboPet {
  @EmbeddedId PetPk pk;
  // This works on existing schemas, but fails while generating schemas with the error:
  // "duplicate column name robot_pet.manufacturer_id in schema"
  @manytoone @joincolumn("manufacturer_id") Manufacturer manufacturerId;
}

@entity class Manufacturer {
  @id string manufacturerId;
}
```

The code above represents the tables:
```
CREATE TABLE robo_pet(name varchar, manufacturer_id varchar);
CREATE TABLE manufacturer(manufacturer_id);
```

The many-to-one relationship is established because many RoboPets share the same "manufacturer_id" column.
This relationship is currently flagged as an error, because the column mentioned in @joincolumn on
RoboPet.manufacturerId is already defined, however, it is a valid relationship and functions on
schemas that have already been created.
@SingingBush SingingBush merged commit 6fc1767 into buggins:master May 14, 2024
39 of 41 checks passed
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.

2 participants