0

I have an application that is written in Laravel. The problem I am facing is that there is another application instance that uses the same information from a different database schema. The idea is to use Laravel's database schema with a new Symfony instance but I am running into an issue that I cannot seem to find a solution for.

Below is a schema of a typical table that exists on laravel's application instance:

CREATE TABLE IF NOT EXISTS `table_name` (
 `id` varchar(255) NOT NULL,
 `payload` text NOT NULL,
 `last_activity` int(11) NOT NULL,
 UNIQUE KEY `sessions_id_unique` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

This table has no primary key recorded into it which I would like to believe Laravel is forgiving when it comes to that. However, Doctrine2, as it seems, does not support reverse engineering on database tables that have no primary key and therefore I cannot create entities from this database due to that.

Is there any hack I can apply to bypass this error as I have hundreds of tables in this database to create my entities manually?

Below is the exception I get from the command line:

[Doctrine\ORM\Mapping\MappingException] Table maw_sessions has no primary key. Doctrine does not support reverse engineering from tables that don't have a primary key.

scruffycoder86
  • 527
  • 1
  • 5
  • 24
  • Don't know a way around the Primary Key, but rather than add all entities manually, couldn't you just add a primary key to each table? id in your example could just have the primary key added for example. – OrderAndChaos Mar 04 '16 at 13:51
  • This question has come up before, see http://stackoverflow.com/questions/8429806/symfony-doctrine-models-for-entity-without-primary-keys it states Doctrine requires every entity class to have an identifier/primary key, but someone has posted a possible hack. – OrderAndChaos Mar 04 '16 at 13:53
  • @ Sarcoma Thanks will check the link out. The Laravel's tables are to many to go through to make sure that every table has a primary key plus wouldn't doing such intriduce unnecessary bugs on the laravel instance application – scruffycoder86 Mar 04 '16 at 14:23
  • I can't think of a reason not to have primary keys on a table, adding a primary key to the id in that example shouldn't do anything except make searches on the id faster. Don't hold me to that though as I'm not a Laravel expert. However there is this "Should each and every table have a primary key?" http://stackoverflow.com/questions/840162/should-each-and-every-table-have-a-primary-key – OrderAndChaos Mar 04 '16 at 14:41
  • In a true sense no table should be without a primary according to my understanding. However, I wanted to be able to reuse this same database instance in extending the management of our applications that would need to make use of the same data contained within this database without touching it. I will try your suggested solution today and confirm if I have gotten it to work. Thanks. – scruffycoder86 Mar 07 '16 at 08:33
  • I understand your dilemma, hope you can work out a solution. – OrderAndChaos Mar 08 '16 at 09:22
  • An [Associative entity or junction table](https://en.wikipedia.org/wiki/Associative_entity) typically has no primary key of its own. – Jacco Jun 08 '16 at 08:38

0 Answers0