Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForeignKey column is not listed in view. #2345

Closed
miettal opened this issue Mar 17, 2023 · 13 comments · Fixed by #2398
Closed

ForeignKey column is not listed in view. #2345

miettal opened this issue Mar 17, 2023 · 13 comments · Fixed by #2398
Labels
bug dependencies Pull requests that update a dependency file fixed-in-next-release

Comments

@miettal
Copy link

miettal commented Mar 17, 2023

from sqlalchemy 2.0.0, It seems foreign key column cannot appear in list-view, edit-view.

Flask-SQLAlchemy==3.0.3
Flask-Admin==1.6.1
sqlalchemy==2.0.6

@miettal
Copy link
Author

miettal commented Mar 17, 2023

Actually I changed sqlalchemy version to sqlalchemy==1.4.46, It works.

@fcollman
Copy link

I think this does indicate that Flask-Admin does not work as one would expect for SqlAlchemy>2.0 and the requirements should be pinned to indicate that or else a patch to make relationship fields work properly out of the box as you would expect.

@BaturinM
Copy link

BaturinM commented May 12, 2023

Have the same issue. It makes impossible using flask-admin with sqlalchemy 2.0 and it's painful!

Here's the code to reproduce:

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
admin = Admin()

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
app.config["SECRET_KEY"] = "secret"


db.init_app(app)
admin.init_app(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String)
    email = db.Column(db.String)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User')


admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(Post, db.session))

with app.app_context():
    db.drop_all()
    db.create_all()

app.run()

Here's requirements:

attrs==22.2.0
blinker==1.6.2
click==8.1.3
Flask==2.3.2
Flask-Admin==1.6.1
Flask-SQLAlchemy==3.0.3
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
packaging==23.0
pluggy==1.0.0
pytest==7.2.2
SQLAlchemy==2.0.13
typing_extensions==4.5.0
Werkzeug==2.3.4
WTForms==3.0.1

There's no any user or user_id in views for Post model:
image

@miettal
Copy link
Author

miettal commented May 19, 2023

I made PR.

@ElLorans
Copy link
Contributor

ElLorans commented May 27, 2023

What happens if you add the foreign key manually?

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
admin = Admin()

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
app.config["SECRET_KEY"] = "secret"


db.init_app(app)
admin.init_app(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String)
    email = db.Column(db.String)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User')

class PostAdmin(ModelView):
    column_list = ('user', 'text')


admin.add_view(PostAdmin(Post, db.session))

with app.app_context():
    db.drop_all()
    db.create_all()

app.run()

@BaturinM
Copy link

@ElLorans then it works properly. But manual adding for every relationship is not what we are used to while using flask admin with sqla < 2.0.

@ElLorans
Copy link
Contributor

I understand, but this workaround makes possible to use SQLAlchemy >2.0, so I thought sharing could be useful.

@BaturinM
Copy link

@ElLorans surely. You did great, thanks! I will use it while waiting for a fix from developers not to use any workarounds for a long time

@ericvanular
Copy link

There is an open PR here that would allow full usage of flask-admin with SQLAlchemy>2.0 - is there something blocking it from being merged?

@stezz
Copy link

stezz commented Oct 23, 2023

Is this also the reason why when using the example below and trying to tap on "Create" will give:

ValueError: not enough values to unpack (expected 4, got 3)

Or is that unrelated?

@cjmayo
Copy link
Contributor

cjmayo commented Oct 25, 2023

Is this also the reason why when using the example below and trying to tap on "Create" will give:

ValueError: not enough values to unpack (expected 4, got 3)

Or is that unrelated?

That looks like WTForms 3.1.0 #2391.

@stezz
Copy link

stezz commented Oct 26, 2023 via email

@alexmarginean16
Copy link

alexmarginean16 commented Feb 15, 2024

I'm so glad I was able to find this issue posted. I have been stuck with this for ages. I was able to implement the workaround where I would specify the column myself but I will have many models and it wouldn't make sense to do that for each of them.

I had to downgrade sqlalchemy to 1.4.46 as mentioned above and also Flask-SQLAlchemy to 3.0.5 so it works with this sqlachemy version.

I hope a fix will be implemented soon.

Edit: It works on the older version but the relationship from the Parent to Child is still not displaying unless you specify the column manually which apparently has been an issue for a longer time.

@samuelhwilliams samuelhwilliams added bug fixed-in-next-release dependencies Pull requests that update a dependency file labels Jul 25, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug dependencies Pull requests that update a dependency file fixed-in-next-release
9 participants