Thanks for your response, Russell.  To follow up on this part of the thread:

Russell Keith-Magee wrote:
>>> I don't believe you can. However, you can use sql or sqlall [1] to
>>> output the table creation sql to a file for editing.  For example:
>>>
>>> manage.py sqlall myapp > myapp.sql
>> I was looking at this, but:  Does anyone know why "manage.py sqlall"
>> rearranges the order of the table-creates from what's in the models?
>>
>> I can't find the reason for the new ordering; it looks kind of random,
>> and it causes the table-creates to fail because of foreign key
constraints.
>
> It isn't random - it's hashtable order. There's no particular reason
> for it, other than the fact that we need to have a dictionary to store
> the models.
>
> However, you shouldn't be having any problems with foreign key
> constraints. The model creation process should be able to identify and
> avoid any problems with forward declared foreign key constraints.

This is what I would guess, but is it possible that "sqlall" produces
different output than is actually used by "syncdb" for single
applications?  "syncdb" on my models.py executes correctly, but "sqlall"
produces output that violates foreign key constraints right away with no
obvious ordering (some tables are created before the tables that they
reference, some are created after).

My backend is MySQL 5.0.22 using InnoDB.

As a contrived example, I hacked down my models.py file to produce the
following test models.py.

In this little model, User is referred to by Computer, and Device refers
to both Computer and DeviceName.

You'll see in the sqlall result that follows that Device is created first,
with a reference to DeviceName.  The Computer reference is correctly
created after the Computer table is created.

Any help would be appreciated here - it would take some work for me to
automate the process of generating custom SQL handlers for each of my
models, but it would be very clean for me to add a step in our dev process
to modify the output of "sqlall" and just use that.

Thanks,
Greg


models.py------------------------

from django.db import models

class User(models.Model):
        id = models.BigIntegerField(primary_key=True)

        class Admin:
                pass

        class Meta:
                db_table='user'

class Computer(models.Model):
        id = models.CharField(primary_key=True, maxlength=16)
        user = models.ForeignKey(User)

        class Admin:
                pass

        class Meta:
                db_table='computer'

class DeviceName(models.Model):
        device_type = models.IntegerField()
        name_index = models.IntegerField()
        name = models.TextField()

        class Admin:
                pass

        class Meta:
                db_table='device_name'

class Device(models.Model):
        name = models.ForeignKey(DeviceName)
        computer = models.ForeignKey(Computer)
        class Admin:
                pass

        class Meta:
                db_table='device'

class DeviceRenderState(models.Model):
        device = models.ForeignKey(Device)
        creator = models.ForeignKey(User)

        class Admin:
                pass

        class Meta:
                db_table='device_render_state'



sqlall result----------------------------------------------

BEGIN;
CREATE TABLE `device` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name_id` integer NOT NULL REFERENCES `device_name` (`id`),
    `computer_id` varchar(16) NOT NULL,
    CONSTRAINT FOREIGN KEY (`name_id`) REFERENCES `device_name` (`id`)
);
CREATE TABLE `device_name` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `device_type` integer NOT NULL,
    `name_index` integer NOT NULL,
    `name` longtext NOT NULL
);
CREATE TABLE `computer` (
    `id` varchar(16) NOT NULL PRIMARY KEY,
    `user_id` bigint NOT NULL REFERENCES `user` (`id`),
    CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
);
ALTER TABLE `device` ADD CONSTRAINT computer_id_referencing_computer_id
FOREIGN
KEY (`computer_id`) REFERENCES `computer` (`id`);
CREATE TABLE `user` (
    `id` bigint NOT NULL PRIMARY KEY
);
CREATE TABLE `device_render_state` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `device_id` integer NOT NULL REFERENCES `device` (`id`),
    `creator_id` bigint NOT NULL REFERENCES `user` (`id`),
    CONSTRAINT FOREIGN KEY (`device_id`) REFERENCES `device` (`id`),
    CONSTRAINT FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`)
);
CREATE INDEX device_name_id ON `device` (`name_id`);
CREATE INDEX device_computer_id ON `device` (`computer_id`);
CREATE INDEX computer_user_id ON `computer` (`user_id`);
CREATE INDEX device_render_state_device_id ON `device_render_state`
(`device_id`
);
CREATE INDEX device_render_state_creator_id ON `device_render_state`
(`creator_i
d`);
COMMIT;


>
> That said, I am aware (and am working on fixing) a slight problem with
> install/sqlall dealing with multiple applications; specificially if
> you try to run:
>
> manage.py sqlall app1 app2
> or
> manage.py install app1 app2
>
> and app1 has a dependency on app2, sqlall reports problems with model
> creation. manage.py syncdb doesn't have this problem.
>
> Is this the problem you are experiencing? If not, can you provide more
> details (backend, models etc)
>
> Yours,
> Russ Magee %-)
>
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to