Comments (20)
problem in 'default=func.now()' & 'onupdate=func.now()' if not use 'func.now()' everything is ok
from sqladmin.
@aminalaee and thanks for library :) I hope you can help anyhow with my issue. File with models in txt
Uploading sample.txt…
from sqladmin.
Hey @xnuinside , Thank you,
Can you please provide the model definition again? You can just paste the minimal example to reproduce this issue?
from sqladmin.
@aminalaee tried upload several times )) not sure why it's not loaded, one more time
sample.txt
from sqladmin.
@aminalaee I will try today to experiment & remove fields till I find that exactly cause a problem, but if you will have ideas to make this investigation more quick :) I will appreciate
from sqladmin.
I'm guessing that might be the server_default of Boolean field which I haven't tested, but it would be quicker if you can provide a minimal example to produce this.
from sqladmin.
@aminalaee problem not in boolean field, so everything works ok if remove DateTime fields:
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
DateTime,
nullable=False,
default=func.now(),
onupdate=func.now(),
)
looks like they cause an issue
from sqladmin.
and if check traceback you can see File "/Users/iuliia_volkova2/Library/Caches/pypoetry/virtualenvs/admin-panel-service-Ji2bEy0B-py3.7/lib/python3.7/site-packages/wtforms/fields/datetime.py", line 36, in _value return self.data and self.data.strftime(self.format[0]) or ""
so problem with datetime field
from sqladmin.
Ok great, that's a good start, feel free to create a PR for it or I'll have a look later 👍
from sqladmin.
@aminalaee I will close the issue, I just recognized, that in python 'default' not 'server_default' somebody put func.now() and this is the reason of issue, if change it to python datetime - everything ok
from sqladmin.
Hello
I have the same issue as @xnuinside
I didn't quite get the solution but it seems to me that my models using func.now() are correct.
If there is an error I don't see why I would need to change my models which are alright.
from sqladmin.
@StorkST
I think the solution was that @xnuinside needed to use server_default and not default. Can you provide a very minimal model to reproduce this?
from sqladmin.
Hi @aminalaee
Sure, here is my code.
I hope this is good for you as an example. I think you might need to create the database with the simple users
table.
So when I try to create a new User on the interface I get the error "Boolean value of this clause is not defined".
main.py file runned with uvicorn main:app --reload
:
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.sql import func
from sqlalchemy.orm import sessionmaker
from fastapi import FastAPI
from sqladmin import Admin, ModelView
from sqlalchemy.ext.declarative import declarative_base
app = FastAPI()
SQLALCHEMY_DATABASE_URL = "mysql://test:test@localhost/test"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# Define admin interface
admin = Admin(app, engine)
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String(40), unique=True, index=True)
creationDate = Column(DateTime, default=func.now())
updateDate = Column(DateTime, onupdate=func.now())
class UserAdmin(ModelView, model=User):
column_list = [User.id, User.email, User.creationDate, User.updateDate]
admin.add_view(UserAdmin)
from sqladmin.
@StorkST func.now() cannot be used in ‘default’ arg. Because it is DB function. default - for Python code that executes before send request to DB. Change func.now() to Python datetime.now() or use it in server_default
from sqladmin.
Yeah As far as I remember this was expected, the default should be populated in the create form, and in the case of func.now() there's no way to set that value upfront. Switching to server_default works around this.
P.S. if you think you can improve this, contributions are welcome.
from sqladmin.
I believe it would be more natural to exclude the func.now() Datetime column from the form.
class UserAdmin(ModelView, model=User):
form_excluded_columns = [ User.creationDate, User.updateDate ]
Because using datetime.now()
may cause delay issues as it returns the time when the code executes. Also, when using server_default
, input box will be displayed on the template and make user set values, which pass the server_default.
It would be convenient if the datetime column is automatically excluded when func_now() is used by including in form_excluded_columns.
Thanks you.
from sqladmin.
Hmm, that might be one solution. But maybe we should exclude any columns with server_default
value, not just with server_default=func.now
. I think that would cover more cases.
from sqladmin.
As far as I test, func.now
, func.current_date
, func.current_time
, func.current_timestamp
, func.localtime
, func.localtimestamp
, and func.sysdate
raise the same issue.
There can be more cases, but isn't it okay to catch only just the functions above? (in my short opinion)
from sqladmin.
I think my previous comment was not very clear.
I meant in the first step we can ignore any fields which have server_default=...
or server_onupdate=...
set so we don't need them in the forms.
But I think modifying the behaviour based on default=...
is going to be a bit complicated.
from sqladmin.
Agreed with you. Thank you for your kindness :)
from sqladmin.
Related Issues (20)
- Static file over https in AWS with ECS and LoadBalancer HOT 16
- CSS and Javascript files for the Admin not loaded HOT 2
- Overriden form text field with FileUpload field. HOT 7
- Not able to edit fields in sql admin when using s3 filetype HOT 3
- Unexpected main page view HOT 6
- Add ModelView.form_query property to create form-filtering feature HOT 2
- override delete model method to perform soft delete HOT 1
- form_overrides doesn't work HOT 2
- SqlAlchemy dataclass support missing HOT 6
- Multiple Admin Instances result in wrong URL's to the views HOT 1
- Ajax query passes incorrect urn when admin mounted on sub fastapi app HOT 3
- Sort symbol is incorrect (wrong direction)
- sqladmin + jinja templates conflict index.html HOT 3
- Raised value error HOT 5
- Define default values via form_args
- TypeError: UUID objects are immutable (fastapi_users) HOT 4
- ValueError on passing unexcepted parameter
- Use HTMX HOT 3
- jinja2.exceptions.TemplateNotFound: list.html HOT 3
- What about Router per View? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqladmin.