Quantcast
Channel: Liquibase Forums
Viewing all articles
Browse latest Browse all 2993

Support AutoIncrement in creating objects

$
0
0
Hi.

I would like to use liquibase not only to install or update a database schema for my application, but also to create some objects using the auto_increment feature of the database and create links between the objects, using their id's. As long as i understand, the current version of liquibase is not supporting this feature.

Let's say we have a person and an address table, with a 1:1 relationship. Both tables are using the auto_increment feature of the database and the table person contains a link to an entry in the adress table, represented by its id, which also has a not null and a foreign key constraint. When i want to create a person with an associated address using liquibase, the only way i see is to create an sql statement, that inserts the address, fetches the id of the newly created address and then inserts the person with the id fo the address or i depend on the execution order and guess the id's. Guessing the id's, the changeset might look like this:

    1. <createTable tableName="address">
    2.   <column name="id" type="int" autoIncrement="true">
    3.     <constraints primaryKey="true" nullable="false"/>
    4.   </column>
    5.   <column name="street" type="varchar(50)" />
    6. </createTable>
    7. <createTable tableName="person">
    8.   <column name="id" type="int" autoIncrement="true">
    9.     <constraints primaryKey="true" nullable="false"/>
    10.   </column>
    11.   <column name="name" type="varchar(50)" />
    12.   <column name="address_id" type="int">
    13.     <constraints references="address" foreignKeyName="id" nullable="false"/>
    14.   </column>
    15. </createTable>

    16. <insert tableName="address">
    17.   <column name="street" value="Sesame street" />
    18. </insert>
    19. <insert tableName="person">
    20.   <column name="name" value="Kermit" />
    21.   <column name="address_id" value="1" />
    22. </insert>
    This is bad code, i think. What if i would like to create entries in an update? There might be already data in these tables and i'm getting in trouble, guessing the wrong id's. Writing SQL statements would even be worse.

    Thinking about better solutions, i thought about creating the schema using liquibase and creating objects in the application, which uses hibernate. This would be pretty simple, because i'm already creating objects in my application. But what about updates? What about error handling and rollback? I'm also a fan of the Single-Responsibility-Principle, which is broken, by doing some database installation/updates using liquibase and some in the application code.

    No, the best solution would be, to create the objects using liquibase, but how can this be done? I thought about something like this:

    1. <insert id="sesame_street" tableName="address">
    2.   <column name="street" value="Sesame street" />
    3. </insert>
    4. <insert tableName="person">
    5.   <column name="name" value="Kermit" />
    6.   <column name="address_id" reference="address" id="sesame_street" />
    7. </insert>
    The first insert gets an optional attribute id, which should be unique inside the change set and according to the table. The second column element of the second insert gets the optional attributes reference and id, containing the referenced table and the given id of the first insert element. I think, this is what a user would like to have. There had to be some constraints, for example, that a reference to an insert statement is limited to the changeset. The reference and the source had to be in the same changeset, because the id can only be determined on creation. Another constraint would be the order of the insert statements. The source had to be inserted before the reference is used.

    Looking at the liquibase api, i thought that it might be possible, to change some sql statements, in order to get the newly created id of an insert statement. Then i have to associate this table id with the given id of the insert element in the changeset. Then i have to extend the column element, where i have to set the right value, if there is a reference to an id field of another table. This is not solving every problem, creating and updating objects, but i think it is a step in the right direction.

    Is this complete non-sense, what i'm thinking about? Is it possible, to write an extension for this? Any ideas, suggestions or comments are appreciated.

    Volker.

    Viewing all articles
    Browse latest Browse all 2993

    Trending Articles



    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>