Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-1318: Unexpectet behavior while using ManyToOne as part ofr composite key #1932

Closed
doctrinebot opened this issue Aug 4, 2011 · 7 comments
Assignees
Labels

Comments

@doctrinebot
Copy link

Jira issue originally created by user pejot:

Hi!

Ran into a problem while wanted to use a ManyToOne as part of a primary key:

 * @Entity
 * @Table(name="user_preferences")
  */
class UserPreferences  {

    /****
     * @Id
     * @ManyToOne(targetEntity="User",cascade={"persist"})
     *  @JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;
    /****
     * @Id
     * @GeneratedValue(strategy="NONE")
     * @Column(name="preference_id",type="smallint",nullable=false)
     */
    protected $preference_id;

By default doctrine creates a table with 2 single keys (the preference key is the primary key) which is of course incorrect.
Had to add @column(name="user_id",type="integer") to the user column to fix the index problem but that introduced another problem. The entity no longer accepter \Entity\User as a value for user and takes only a smallint as defined.

@doctrinebot
Copy link
Author

Comment created by @beberlei:

This is probably because of the @GeneratedStrategy annotation, can you try to just remove it? For me there is always only one key generated.

@doctrinebot
Copy link
Author

Comment created by pejot:

Hi,
thanks fo the fast reply

     * @Id
     * @ManyToOne(targetEntity="User",cascade={"persist"})
     *  @JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

     * 
     * @Id
     * @Column(name="preference_id",type="smallint",nullable=false)
     */
    protected $preference_id;

On MySQL creates:

mysql> show indexes from xxx.user_preferences;
<ins>------------------</ins>------------<ins>------------------------------</ins>--------------<ins>---------------</ins>-----------<ins>-------------</ins>----------<ins>--------</ins>------<ins>------------</ins>---------<ins>
| Table            | Non*unique | Key_name                     | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index*type | Comment |
</ins>------------------<ins>------------</ins>------------------------------<ins>--------------</ins>---------------<ins>-----------</ins>-------------<ins>----------</ins>--------<ins>------</ins>------------<ins>---------</ins>
| user*preferences |          0 | PRIMARY                      |            1 | preference*id | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| user*preferences |          1 | user_preferences_user_id_idx |            1 | user*id       | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |
<ins>------------------</ins>------------<ins>------------------------------</ins>--------------<ins>---------------</ins>-----------<ins>-------------</ins>----------<ins>--------</ins>------<ins>------------</ins>---------<ins>
2 rows in set (0.01 sec)

With:

     * @Id
     * @ManyToOne(targetEntity="User",cascade={"persist"})
     *  @JoinColumn(name="user_id", referencedColumnName="id")
     * @Column(name="user_id",type="integer")
     */
    protected $user;


     * @Id
     * @GeneratedValue(strategy="NONE")
     * @Column(name="preference_id",type="smallint",nullable=false)
     */
    protected $preference_id;

Creates:

mysql> show indexes from xxx.user_preferences;
</ins>------------------<ins>------------</ins>----------<ins>--------------</ins>---------------<ins>-----------</ins>-------------<ins>----------</ins>--------<ins>------</ins>------------<ins>---------</ins>
| Table            | Non*unique | Key_name | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index*type | Comment |
<ins>------------------</ins>------------<ins>----------</ins>--------------<ins>---------------</ins>-----------<ins>-------------</ins>----------<ins>--------</ins>------<ins>------------</ins>---------<ins>
| user*preferences |          0 | PRIMARY  |            1 | user*id       | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| user*preferences |          0 | PRIMARY  |            2 | preference*id | A         |           0 |     NULL | NULL   |      | BTREE      |         |
</ins>------------------<ins>------------</ins>----------<ins>--------------</ins>---------------<ins>-----------</ins>-------------<ins>----------</ins>--------<ins>------</ins>------------<ins>---------</ins>
2 rows in set (0.00 sec)

The annotations:

     * @Id
     * @ManyToOne(targetEntity="User",cascade={"persist"})
     *  @JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

     * @Id
     * @Column(name="preference_id",type="smallint",nullable=false)
     */
    protected $preference_id;

Create the same effect like the first one:

mysql> show indexes from xxx.user_preferences;
<ins>------------------</ins>------------<ins>------------------------------</ins>--------------<ins>---------------</ins>-----------<ins>-------------</ins>----------<ins>--------</ins>------<ins>------------</ins>---------<ins>
| Table            | Non*unique | Key_name                     | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index*type | Comment |
</ins>------------------<ins>------------</ins>------------------------------<ins>--------------</ins>---------------<ins>-----------</ins>-------------<ins>----------</ins>--------<ins>------</ins>------------<ins>---------</ins>
| user*preferences |          0 | PRIMARY                      |            1 | preference*id | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| user*preferences |          1 | user_preferences_user_id_idx |            1 | user*id       | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |
<ins>------------------</ins>------------<ins>------------------------------</ins>--------------<ins>---------------</ins>-----------<ins>-------------</ins>----------<ins>--------</ins>------<ins>------------</ins>---------+
2 rows in set (0.00 sec)

Sequence of events :

  1. change in annotations in entity object
  2. dropped the table affected
  3. orm:schema-tool:update --force

@doctrinebot
Copy link
Author

Comment created by @beberlei:

Formatted code.

@doctrinebot
Copy link
Author

Comment created by @beberlei:

Can you drop the table and show the DDL generated by both "orm:schema-tool:create --dump-sql" and "orm:schema-tool:update --dump-sql" ?

I checked with our testsuite and all the entities generate the correct primary keys.

@doctrinebot
Copy link
Author

Comment created by @Ocramius:

Is this still valid with newer versions of the ORM?

@doctrinebot
Copy link
Author

Comment created by @beberlei:

No feedback given

@doctrinebot
Copy link
Author

Issue was closed with resolution "Incomplete"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants