Doctrine orm:convert-mappings errors on primary key

Doctrine error orm:convert-mappings

Doctrine orm error like above? I have been there.

The solution I am providing is not that great solution but it can keep you going for a while.

A tool for generating entities for doctrine orm from the database table without primary key might complain as:

has no primary key. Doctrine does not support reverse engineering from tables that don’t have a primary key.

From the code base search for DatabaseDriver.php file:

Go to DatabaseDriver.php

Search for reverseEngineerMappingFromDatabase method in the given file.

uncomment the the following part as follows

           //if ( ! $table->hasPrimaryKey()) {
              //  throw new MappingException(
                //    "Table " . $table->getName() . " has no primary key. Doctrine does not ".
                  //  "support reverse engineering from tables that don't have a primary key."
                //);
            //}

            //$pkColumns = $table->getPrimaryKey()->getColumns();

Add the following line

$pkColumns = array();

Then search for method getTablePrimaryKeys and comment everything inside it and return empty array.

The above method will remove the has no primary key. Doctrine does not support reverse engineering from tables that don’t have a primary key. error when using entity generation tool.

Adding auto_increment after table creation

Say the table is created like this,

mysql> CREATE TABLE customer (id int, fname varchar(20));

The above table would be created without having an index. If there is a need to make id primary key, then

mysql> ALTER TABLE customer ADD PRIMARY KEY (id);

Then adding auto_increment would be as:

mysql> ALTER TABLE customer CHANGE id id int NOT NULL AUTO_INCREMENT;

Trying to assign auto_increment before making it a primary key – if it is not already done during creation, might produce an error. Actually trying to drop the primary key before neutralizing the auto_increment would also might create an error.
The following query would take out the auto_increment part from the table

ALTER TABLE customer CHANGE id id int not null;

And, of course, after this it is possible to drop primary key.

How to add and drop primary key in SQL Server

If you want to add primary key after you create the table in the sql server, then
1. go to Microsoft SQL Server Management Studio
2. Right click on the database and select new query
3. ALTER TABLE tbl_name ADD PRIMARY KEY(pr_key_column);
tbl_name – name of the table you want to assign primary key
pr_key_column – column/field name which you want it to be primary key

If the pr_key_column is allowed null values, then you can’t assign it as primary key. So if that is the case use this query first:
ALTER TABLE tbl_name ALTER COLUMN pr_key_column datatype not null;

To revoke primary key:
ALTER TABLE tbl_name DROP CONSTRAINT(pr_key_column);