You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Alex Epshteyn <al...@gmail.com> on 2005/09/21 06:38:03 UTC

Foreign keys & inability to control CREATE TABLE order cause errors MySQL

MySQL gives me an error when Torque attempts to execute my generated
.sql file.  This happens because the a CREATE TABLE statement has a
foreign key pointing to a table that hasn't been created yet.

There are two possible ways I can think of to resolve this problem,
but don't know how to implement them in the XDoclet/Torque framework. 
Perhaps someone could help.

1.  Insert "SET FOREIGN_KEY_CHECKS = 0;" at the beginning of and "SET
FOREIGN_KEY_CHECKS = 1;" at the end of the project-schema.sql file. 
Is there any way to get Torque to do this when it generates that file?

2.  Control the order in which XDoclet generates the table
descriptions in the project-schema.xml file.  As far as I know there
is no way of doing this.  Is there?

All other solutions I can think of would be hacks.

Thanks,
Alex

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Foreign keys & inability to control CREATE TABLE order cause errors MySQL

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/21/05, Alex Epshteyn <al...@gmail.com> wrote:
> MySQL gives me an error when Torque attempts to execute my generated
> .sql file.  This happens because the a CREATE TABLE statement has a
> foreign key pointing to a table that hasn't been created yet.
> 
> There are two possible ways I can think of to resolve this problem,
> but don't know how to implement them in the XDoclet/Torque framework.
> Perhaps someone could help.
> 
> 1.  Insert "SET FOREIGN_KEY_CHECKS = 0;" at the beginning of and "SET
> FOREIGN_KEY_CHECKS = 1;" at the end of the project-schema.sql file.
> Is there any way to get Torque to do this when it generates that file?
> 
> 2.  Control the order in which XDoclet generates the table
> descriptions in the project-schema.xml file.  As far as I know there
> is no way of doing this.  Is there?
> 
> All other solutions I can think of would be hacks.

There is a third solution. MySQL (as every other database that I know)
is able to add foreign keys via ALTER TABLE statements. Eg. first all
tables are created, and then they are altered to add the foreign keys.
You could try DdlUtils (http://db.apache.org/ddlutils) which does
exactly this and is compatible to the Torque schema XML.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Foreign keys & inability to control CREATE TABLE order cause errors MySQL

Posted by Alex Epshteyn <al...@gmail.com>.
Thanks, Thomas.  I'll give DdlUtils a try sometime.

- Alex

On 9/21/05, Thomas Dudziak <to...@gmail.com> wrote:
> The DdlUtils website is slightly out of date (should be fixed this
> week), here's how you'd use it in your Ant build file (with PostgreSQL
> in this example, but it is similar with MySQL):
>
>     <taskdef name="ddlToDatabase"
>             classname="org.apache.ddlutils.task.DdlToDatabaseTask">
>      <classpath refid="runtime-classpath"/>
>    </taskdef>
>
>    <ddlToDatabase>
>      <database url="jdbc:postgresql://localhost/test"
>                driverClassName="org.postgresql.Driver"
>                username="admin"
>                password="superDuperPassword"/>
>      <fileset dir="${build.database.dir}" includes="*-schema.xml"/>
>
>      <createdatabase failonerror="false"/>
>      <writeschematodatabase alterdatabase="false"/>
>    </ddlToDatabase>
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Foreign keys & inability to control CREATE TABLE order cause errors MySQL

Posted by Thomas Dudziak <to...@gmail.com>.
The DdlUtils website is slightly out of date (should be fixed this
week), here's how you'd use it in your Ant build file (with PostgreSQL
in this example, but it is similar with MySQL):

    <taskdef name="ddlToDatabase"
            classname="org.apache.ddlutils.task.DdlToDatabaseTask">
     <classpath refid="runtime-classpath"/>
   </taskdef>

   <ddlToDatabase>
     <database url="jdbc:postgresql://localhost/test"
               driverClassName="org.postgresql.Driver"
               username="admin"
               password="superDuperPassword"/>
     <fileset dir="${build.database.dir}" includes="*-schema.xml"/>

     <createdatabase failonerror="false"/>
     <writeschematodatabase alterdatabase="false"/>
   </ddlToDatabase>

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org