What are the Slack Archives?

Itโ€™s a history of our time together in the Slack Community! Thereโ€™s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you wonโ€™t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hi everyone, how can I create a self-referencing one-to-one relationship? ```

U01LE4BMBK7
U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

Hi everyone,
how can I create a self-referencing one-to-one relationship?

<table name="spy_tax_rate">
        <column name="fk_parent_tax_rate" type="INTEGER" required="false"/>

        <foreign-key name="spy_tax_rate-fk_parent_tax_rate" foreignTable="spy_tax_rate" phpName="ParentTaxRate" refPhpName="ChildTaxRate">
            <reference local="fk_parent_tax_rate" foreign="id_tax_rate"/>
        </foreign-key>
</table>

creates a one-to-many relationship.

Comments

  • Eugenia Poidenko
    Eugenia Poidenko Sprykee Posts: 145 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    Hi. Will making the fk unique do the job?

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    I tried...it didn't

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet
    edited February 2021

    I added


    but it didn't change anything

  • Eugenia Poidenko
    Eugenia Poidenko Sprykee Posts: 145 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    you mean that after running the migration, you are still able to add duplicated values to the column? ๐Ÿ˜ฎ

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    at least SpyTaxRate only has a method getChildTaxRates that gives me a collection instead of a single entity

  • Eugenia Poidenko
    Eugenia Poidenko Sprykee Posts: 145 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet
    edited February 2021

    this seems to be a propel limitation& I see no way to do it except one http://propelorm.org/documentation/04-relationships.html#one-to-one-relationships

    here is someone with the same problem https://github.com/propelorm/Propel2/issues/1187

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    yes, I saw that

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    that's not so great

  • Eugenia Poidenko
    Eugenia Poidenko Sprykee Posts: 145 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    so if your fk is the primary key for the table, it will work. Agreed, not fun at all

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet
        <table name="spy_tax_rate">
            <column name="id_tax_rate" type="INTEGER" primaryKey="true" autoIncrement="true"/>
            <column name="fk_parent_tax_rate" type="INTEGER" required="false"/>
    
            <foreign-key name="spy_tax_rate-fk_parent_tax_rate" foreignTable="spy_tax_rate">
                <reference local="fk_parent_tax_rate" foreign="id_tax_rate"/>
            </foreign-key>
    
        </table>
    

    it is the primary key...doesn't create a one-to-one relationship though

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    you can put this in
    src/Pyz/Zed/Tax/Persistence/Propel/Schema/spy_tax.schema.xml
    and test yourself

  • U01LE4BMBK7
    U01LE4BMBK7 Posts: 241 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    I just ran docker/sdk console propel:install - I think that's enough!?