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
Behaviour of flush change in v5 when updating an entity property with a new entity #2781
Comments
Sounds like you oversimplified the repro. Let me guess, you have also 1:m inverse side defined for that? Sounds like you are missing |
I tested it with exactly the entities i posted above, nothing else. And why would i need orphanRemoval? I don't have a problem with removing the old entity, but updating the customer with the new address that i created. This is my config: const config: Options = {
debug: false,
entities: [Address, Customer],
type: 'postgresql',
clientUrl: process.env.DATABASE_URI,
password: process.env.DATABASE_PASSWORD,
metadataProvider: TsMorphMetadataProvider,
}; migration: export class Migration20220217151832 extends Migration {
async up(): Promise<void> {
this.addSql('create table "address" ("id" uuid not null default gen_random_uuid(), "created_at" timestamptz(0) not null, "updated_at" timestamptz(0) not null, "company_name" varchar(255) not null);');
this.addSql('create index "address_created_at_index" on "address" ("created_at");');
this.addSql('alter table "address" add constraint "address_pkey" primary key ("id");');
this.addSql('create table "customer" ("id" uuid not null default gen_random_uuid(), "created_at" timestamptz(0) not null, "updated_at" timestamptz(0) not null, "customer_number" varchar(255) not null, "company_address_id" uuid not null);');
this.addSql('create index "customer_created_at_index" on "customer" ("created_at");');
this.addSql('create index "customer_customer_number_index" on "customer" ("customer_number");');
this.addSql('alter table "customer" add constraint "customer_customer_number_unique" unique ("customer_number");');
this.addSql('create index "customer_sorted_id" on "customer" ("id", "created_at");');
this.addSql('alter table "customer" add constraint "customer_pkey" primary key ("id");');
this.addSql('alter table "customer" add constraint "customer_company_address_id_foreign" foreign key ("company_address_id") references "address" ("id") on update cascade;');
}
async down(): Promise<void> {
this.addSql('alter table "customer" drop constraint "customer_company_address_id_foreign";');
this.addSql('drop table if exists "address" cascade;');
this.addSql('drop table if exists "customer" cascade;');
}
} Extending my address definition with reverse site and orphanRemoval like this also does not help: @Entity()
export class Address extends BaseEntity {
@Property()
companyName: string;
@OneToMany(() => Customer, (customer) => customer.companyAddress, { orphanRemoval: true })
customers = new Collection<Customer>(this);
constructor(companyName: string) {
super();
this.companyName = companyName;
}
} |
The fix i did for now is that i call flush after i created the new address and then flush again after i update the customer, this works. But i would rather call flush at the end of everything, so it's an all-or-nothing-transaction. |
Interesting, the issue is with the database generated UUID. If you provide the value on runtime, it is passing. So it actually tries to set the relation before creating the address.
|
This is soo weird. It is actually caused by the |
Testet it without onUpdate and it also works for me, really strange ... i also opened another issue related to onUpdate method, not sure it they are somehow related to the same change that happened with v5. |
Describe the bug
If i have a customer entity in the database already with an address, an then set a new Address entity, that is not already in the DB on the address property and call flush, i get the Error below. In v4 this was working.
Stack trace
To Reproduce
Minimal example:
Versions
The text was updated successfully, but these errors were encountered: