diem-project/diem

Loremization: SQL Integrity constraint violation record extending another and having DmMedia relations

antitoxic opened this issue · 12 comments

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (ethnologia.media, CONSTRAINT media_image_id_dm_media_id FOREIGN KEY (image_id) REFERENCES dm_media (id))

Schema:
Media:
actAs:
DmSortable: { new: last }
Timestampable:
I18n:
fields: [caption]
columns:
caption:
type: string(255)
notnull: true
is_displayed_on_home:
type: boolean
default: false

Video:
    inheritance:
        type:             column_aggregation
        extends:          Media
    columns:
        preview_id:
            type: integer
        video_id:
            type: integer
        gallery_id:
            type: integer
    relations:
        Preview:
            class:      DmMedia
            local:      preview_id
            onDelete:   RESTRICT
        Video:
            class:      DmMedia
            local:      video_id
            onDelete:   RESTRICT
        Gallery:
            local:      gallery_id
            onDelete:   CASCADE


Photo:
    inheritance:
        type:             column_aggregation
        extends:          Media
    columns:
        image_id:
            type: integer
        gallery_id:
            type: integer

I think this is because getAllColumns() calls getColumns() which returns ALL columns from EVERY upper and down classes.

That's why you can't loremize a Photo table, because it returns columns which have nothing to do with the class in question.

I'm trying to fix it.

Thank you,

Regards

That's why I've modified the dmDoctrineFormGenerator->getColumns() method by adding some fields to get valuable generated forms with good inheritance tree.

I think I have to move this code to dmDoctrineTable, then make the dmDoctrineFormGenerator directly call dmDoctrineTable instead of having this logic within it (which doesnt make sense) :)

Thank you !

Just found the very same thing. I even think it should be part of Doctrine package itself but for now instead of waiting for Doctrine (which is already focused on version 2) we should include it in dmDoctrineTable.

Hmm..
Something isn't right.
Can't Diem simply generate the models more efficiently? I mean when you define the schema in the YAML it is obvious that the sublasses don't have such ridiculous properties.

Why all columns are defined in the parent class ? Can't the subclasses simply override setTableDefinition and add their fields there. There will be no complications in this manner.

Yes but Diem doesnt overload the doctrine model generation process.

For now I've spent 10 minutes and looks like the problem comes form Doctrine_Import_Schema->_processInheritance($array);

I think I understand why it is behaving this way: because the top class needs to have all columns to build the sql table definition and create the sql to insert table.

Hey, I think doing so will lead us too far in the revamping of Doctrine.

For now I've been able to generate Base*Table correctly, having the BaseVideoTable->setTableDefinition() written correctly, with the columns, instead of having them up to its Media table def.

But we are getting in trooble when we want to build the DB.
I would have to revamp all the Doctrine_Export_* classes which is kind of hell to do.

I don't know yet, it looks like too big to do for now.

I'd prefer to revamp the getColumn() method of dmDoctrineTable, in another way:
if $table is inheriting, return only columns from this very class, else return all columns.

As I've done in form generator class.

What do you thing ?

In fact it looks like we can manage this by modifying
Doctrine_Export->exportClassesSql(),
and adding fields from subclasses when class have subclasses (following what I've done so far).

I've pushed my actual work on diem-project/doctrine in a new branch :
https://github.com/diem-project/doctrine/tree/fix-inheritance-models

Model generation is working right.
DB sql is working right.

Now I've issues when calling loremize. I've stop here. Will continue tomorrow.

Regards,

I've been working on projects non-stop for the last few days. Sorry for responding late.
I'll have a look at this. If we fix this actually it might help us in many directions.
Because we are not only fixing Loremizing but the wierd logic which is likely to cause more bugs for someone that doesn't know how Doctrine works.

Hey, don't worry ;)

I did not continue today, brain pause =) I've worked on some sf2-git stuff, refreshing brain. Anyway.

I think to problem I encounter comes from the fact that there are side effects on the mods I've made. Model generation is great, db sql is generated, AFAIK, in a good way (FKs are merged down-top the inheritance tree; indexes too, but did not test it all).

So, the problem might come of how the DQL is being built. And I'm pretty sure might end up in a big challenge =)

Regards,