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:
- <createTable tableName="address">
- <column name="id" type="int" autoIncrement="true">
- <constraints primaryKey="true" nullable="false"/>
- </column>
- <column name="street" type="varchar(50)" />
- </createTable>
- <createTable tableName="person">
- <column name="id" type="int" autoIncrement="true">
- <constraints primaryKey="true" nullable="false"/>
- </column>
- <column name="name" type="varchar(50)" />
- <column name="address_id" type="int">
- <constraints references="address" foreignKeyName="id" nullable="false"/>
- </column>
- </createTable>
-
- <insert tableName="address">
- <column name="street" value="Sesame street" />
- </insert>
- <insert tableName="person">
- <column name="name" value="Kermit" />
- <column name="address_id" value="1" />
- </insert>
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:
- <insert id="sesame_street" tableName="address">
- <column name="street" value="Sesame street" />
- </insert>
- <insert tableName="person">
- <column name="name" value="Kermit" />
- <column name="address_id" reference="address" id="sesame_street" />
- </insert>
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.