I
I
Igor2020-10-07 22:12:11
MySQL
Igor, 2020-10-07 22:12:11

How to avoid an explicit primary key?

My question is how to avoid an explicit primary key.

The ContactHistory entity will fill up very quickly and there is a chance that the index will run out (I'm exaggerating)
I just want to save, why use an explicit primary key if it will never be used

Yes, you can use a composite primary key, but I do not see fields that would guarantee uniqueness .

/**
 * @ORM\Entity(repositoryClass=ContactRepository::class)
 * @ORM\Table(name="contacts")
 */
class Contact
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="bigint")
     */
    private ?int $id;

    ...

    /**
     * @var ArrayCollection|PersistentCollection
     * @ORM\OneToMany(targetEntity="ContactHistory", mappedBy="contact", cascade={"remove"}, fetch="EXTRA_LAZY")
     */
    private $history;
    ...
}



/**
 * @ORM\Entity(repositoryClass=ContactHistoryRepository::class)
 * @ORM\Table(name="contacts_history")
 */
class ContactHistory
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="bigint")
     */
    private int $id;

    /**
     * @var Contact|null
     * @ORM\ManyToOne(targetEntity="Contact", inversedBy="history", cascade={"persist"})
     */
    private ?Contact $contact;
    ...
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2020-10-07
@IgorPI

I did it according to this article
So far, only so that the doctrine would calm down

A
alexalexes, 2020-10-07
@alexalexes

You can make a local history id unique within a single contact.
It turns out that there will be a unique contact id + history id. You will spread the capacity of one identifier into two.
You can sort, you can find the desired history record. Which history record appeared earlier for neighboring contacts - most likely it is impossible to find out if there is no end-to-end id.
At first glance, such opportunities and disadvantages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question