ahmetb / orman

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)
Other
249 stars 47 forks source link

Entity Dependency Tree plugin #14

Closed ahmetb closed 13 years ago

ahmetb commented 13 years ago

Currently, creation of tables are ordered by entities with less foreign keys first principle and during deletion of tables the policy is delete entities with more foreign keys first in order not to break foreign key constraints. This is very poor.

This policy will not always work and implemented very earlier just to get things work – a nasty hack.

Consider the following tables:

Table A --> FK: A.X references C.X Table B --> FK: B.Y references A.X Table C Table D--> FK: D.Z references C.Z & D.W references C.W.

With our algorithm the possible creation orders (first to last):

however if the first schedule is chosen, it won't work because when attempted to create B we say

CREATE TABLE B ...(..., FOREIGN KEY B.Y REFERENCES A.X)

however A does not exist, yet.

We need to implement a dependency tree plugin (which is constructed just before calling createScheme() in MappingSession.start(). It will help us have safe way to create and drop tables according to SchemeCreationPolicy.

The enough information can be obtained from MappingSession.scheme.getEntities() --> List<Entity> --> e.getFields() --> Field --> (f.isForeignKey(), f.getClazz()`).

ahmetb commented 13 years ago

Fixed by implementing org.orman.mapper.EntityDependencyGraph. Works awesome.