• Florian Sachs's avatar
    process inherited primary keys correctely · 3d897ee8
    Florian Sachs authored
    When using joined table inheritance (http://docs.sqlalchemy.org/en/latest/orm/inheritance.html), it is common practice to name the pk-property the same like the pk-property of the parent. The child-property ist a pk itself and has a foreign-key relationship to the pk-property of the parent.
    
    Example:
    
        class BaseWahl(Model):
            __tablename__ = 'basewahlen'
    
            id = Column(Integer, primary_key=True, autoincrement=True)
            discriminator = db.Column(db.String(50))
    
            __mapper_args__ = {
            'polymorphic_identity':'basewahl',
            'polymorphic_on': discriminator
            }
    
        class Wahl(BaseWahl):
            __tablename__ = 'wahlen'
    
            id = Column(Integer, ForeignKey('basewahlen.id'), primary_key=True)
            __mapper_args__ = {
                'polymorphic_identity':'wahl',
            }
    
    `AdminModelConverter.convert()` does not allow Properties with multiple columns, but will raise a `TypeError`. I changed it into the following way:
    
    - If
      - more than 1 column for the property
      - all columns are primary keys
      - only *one* does not have a Foreign key
      - only one column corresponds to the current model
    - select the column, that corresponds to the current model
    
    I applied the same code to `ModelView.scaffold_list_columns()` and extended it, the primary key in this special constellation actually is *not* ignored, even if it has a foreign key property, so it can be displayed using `column_display_pk = True`.
    
    This solution actually works for me. I do not have enough insight into *Flask-Admin* and definitely even less into *sqlalchemy* to think my solution is the correct one for every situation, but it may be something to think about.
    3d897ee8
Name
Last commit
Last update
babel Loading commit data...
doc Loading commit data...
examples Loading commit data...
flask_admin Loading commit data...
.gitignore Loading commit data...
.gitmodules Loading commit data...
.travis.yml Loading commit data...
AUTHORS Loading commit data...
LICENSE Loading commit data...
MANIFEST.in Loading commit data...
Makefile Loading commit data...
NOTICE Loading commit data...
README.rst Loading commit data...
TODO.txt Loading commit data...
projects.rst Loading commit data...
requirements.txt Loading commit data...
setup.cfg Loading commit data...
setup.py Loading commit data...