Skip to content

Reverse : do not create foreign keys for excluded tables  #1546

Open
@cailholm

Description

@cailholm

When reversing a database and exclude_tables exists in config file, foreign keys to theses tables are still created. This cause errors during models build command.

For example:
user
id
name
group_id
another_table_id

group
id
role

another_table
id
a_field

In the config file, I exclude another_table:
'exclude_tables' => [ 'another_table', ]

php artisan propel:reverse:database with laravel gives me this schema.xml

<?xml version="1.0" encoding="utf-8"?>
<database name="default" defaultIdMethod="native" defaultPhpNamingMethod="underscore">
  <table name="group" idMethod="native" phpName="Group">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="role" phpName="Role" type="VARCHAR" size="20" required="true"/>
    <vendor type="mysql">
      <parameter name="Engine" value="InnoDB"/>
    </vendor>
  </table>
  <table name="user" idMethod="native" phpName="User">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="name" phpName="Name" type="VARCHAR" size="20" required="true"/>
    <column name="group_id" phpName="GroupId" type="INTEGER" required="true"/>
    <column name="another_table_id" phpName="AnotherTableId" type="INTEGER" required="true"/>
    <foreign-key foreignTable="group" name="user_ibfk_1">
      <reference local="group_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="another_table" name="user_ibfk_2">
      <reference local="another_table_id" foreign="id"/>
    </foreign-key>
    <index name="group_id">
      <index-column name="group_id"/>
    </index>
    <index name="another_table_id">
      <index-column name="another_table_id"/>
    </index>
    <vendor type="mysql">
      <parameter name="Engine" value="InnoDB"/>
    </vendor>
  </table>
</database>

A foreign key for another_table in user table has been created.
When I try to build the models, I get an exception

php artisan propel:model:build

   Symfony\Component\Debug\Exception\FatalThrowableError  : Call to a member function hasCompositePrimaryKey() on null

  at /Data/(..)/ulysse-api/vendor/propel/propel/src/Propel/Generator/Model/Table.php:789
    785|             }
    786| 
    787|             // check for incomplete foreign key references when foreign table
    788|             // has a composite primary key
  > 789|             if ($foreignTable->hasCompositePrimaryKey()) {
    790|                 // get composite foreign key's keys
    791|                 $foreignPrimaryKeys = $foreignTable->getPrimaryKey();
    792|                 // check all keys are referenced in foreign key
    793|                 foreach ($foreignPrimaryKeys as $foreignPrimaryKey) {

  Exception trace:

  1   Propel\Generator\Model\Table::setupReferrers()
      /Data/(..)/ulysse-api/vendor/propel/propel/src/Propel/Generator/Model/Database.php:835

  2   Propel\Generator\Model\Database::setupTableReferrers()
      /Data/(..)/ulysse-api/vendor/propel/propel/src/Propel/Generator/Model/Database.php:802

  Please use the argument -v to see more details.

If I remove the foreign-key entry from schema.xml, models are built normally.

Excepted behaviour of reverse command should be not creating foreign keys of excluded tables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions