So I have these entities:
import {
Entity,
PrimaryColumn,
Column,
OneToMany,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm'
import { Address } from './address'
import { Text } from './text'
/**
* Represents the aggregation of ENS properties on the database.
* See docs: https://docs.ens.domains/web/quickstart
*/
@Entity()
export class Domain {
@PrimaryColumn({ unique: true })
node: string
@Column({ nullable: true, length: 32 })
contenthash?: `0x${string}`
@Column({ type: 'bigint' })
ttl: number
@OneToMany(() => Address, (addr) => addr.domain, { cascade: true })
addresses: Address[]
@OneToMany(() => Text, (text) => text.domain, { cascade: true })
texts: Text[]
@Column({ type: 'varchar' })
owner: string
@CreateDateColumn({ default: 'now' })
createdAt?: Date
@UpdateDateColumn({ default: 'now' })
updatedAt?: Date
}
import {
Entity,
PrimaryColumn,
Column,
ManyToOne,
JoinColumn,
CreateDateColumn,
UpdateDateColumn,
Unique,
} from 'typeorm'
import { Domain } from './domain'
/**
* Represents an Text entity on the database.
* See docs: https://docs.ens.domains/web/records
*/
@Entity()
@Unique(['key', 'domain.node'])
export class Text {
@PrimaryColumn()
key: string
@Column()
value: string
@JoinColumn({ name: 'domain', referencedColumnName: 'node' })
@ManyToOne(() => Domain, (domain) => domain.texts)
domain: Domain
@CreateDateColumn({ default: 'now' })
createdAt?: Date
@UpdateDateColumn({ default: 'now' })
updatedAt?: Date
}
What I wanna do is to have only a single key per domain on the Text
table. I’m using upsert for this but it throws 'QueryFailedError: duplicate key value violates unique constraint "PK_99ea12b7a9f78f06d7ba4674348"
.
The upsert call:
await this.client.getRepository(Text).upsert(
{
key,
value,
domain: {
node,
},
},
['domain.node', 'key'],
)
I see the node having a different value but it says the unique constraint is being violated anyway.
I’ve tried adding the @Unique(['key', 'domain.node'])
on the Text entity which wasn’t present before. I also tried to use `[‘domain’, ‘key’] but it also didn’t work.
I expect only a single key
per domain.node
on the Text table.