0

After running the code without any error the following code will causes an InvalidRequestError when pushing the "Edit record" or "Delete record" button in the User tab. This error is raised when using inheritance as demonstrated in code. I think it's a bug in Flask-Admin; Does anybody have an idea??
InvalidRequestError: Incorrect number of values in identifier to formulate primary key for query.get(); primary key columns are 'identities.global_id'
These are the Flask versions which I've used:

  • Flask: 0.10.1
  • Flask-Admin: 1.0.7
  • Flask-SQLAlchemy: 1.0

import os
import os.path as op
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

from flask.ext import admin
from flask.ext.admin.contrib import sqla

app = Flask(__name__)
app.config['SECRET_KEY'] = '123456790'
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)

# Model with inheritance
###########################
class Identity(db.Model):
    __tablename__ = 'identities'
    global_id = db.Column(db.Integer, primary_key=True)
    _dto_type = db.Column('dto_type', db.String, nullable=False)
    __mapper_args__ = {'polymorphic_on': _dto_type}


class User(Identity):
    __mapper_args__ = {'polymorphic_identity': 'Operator'}
    id = db.Column(db.ForeignKey('identities.global_id'), primary_key=True)
    name = db.Column(db.String(100))

    def __str__(self):
        return self.username

# Views
#######
@app.route('/')
def index():
    return '<a href="/admin/">Click me to get to Admin!</a>'

# Create admin
admin = admin.Admin(app, 'Simple Models')
admin.add_view(sqla.ModelView(User, db.session))

if __name__ == '__main__':
    db.create_all()
    u = User(name='TestUser')
    db.session.add(u)
    db.session.commit()
    app_dir = op.realpath(os.path.dirname(__file__))
    database_path = op.join(app_dir, app.config['DATABASE_FILE'])
    app.run(debug=True, port=5003)
met1366
  • 3
  • 1
  • 3
  • When posting an error, it is helpful if you post the full traceback. When you see the error in the error page, you can click the "Traceback" header to get a "Copy/Paste friendly version" of the traceback, which you can then put into your question. – Mark Hildreth Jan 24 '14 at 00:22

1 Answers1

1

mrjoes (see here): "Flask-Admin has limited support for inherited model. I'm going to work on improved inheritance support, but right now is very busy.

In this particular case, Flask-Admin detects multi-primary key model (User has two primary keys - id and inherited global_id) and attempts to pass them to SQLAlchemy when making query."

Philipp der Rautenberg
  • 2,212
  • 3
  • 25
  • 39
  • Man's a god, thank you so much. I don't even know why I specified two primary keys but I was tearing hairs out trying to figure out why I was getting this bug. – Sheng May 03 '21 at 12:10