Coder Social home page Coder Social logo

Comments (20)

xnuinside avatar xnuinside commented on June 20, 2024 1

problem in 'default=func.now()' & 'onupdate=func.now()' if not use 'func.now()' everything is ok

from sqladmin.

xnuinside avatar xnuinside commented on June 20, 2024

@aminalaee and thanks for library :) I hope you can help anyhow with my issue. File with models in txt
Uploading sample.txt…

from sqladmin.

aminalaee avatar aminalaee commented on June 20, 2024

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.

xnuinside avatar xnuinside commented on June 20, 2024

@aminalaee tried upload several times )) not sure why it's not loaded, one more time
sample.txt

from sqladmin.

xnuinside avatar xnuinside commented on June 20, 2024

@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.

aminalaee avatar aminalaee commented on June 20, 2024

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.

xnuinside avatar xnuinside commented on June 20, 2024

@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.

xnuinside avatar xnuinside commented on June 20, 2024

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.

aminalaee avatar aminalaee commented on June 20, 2024

Ok great, that's a good start, feel free to create a PR for it or I'll have a look later 👍

from sqladmin.

xnuinside avatar xnuinside commented on June 20, 2024

@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.

StorkST avatar StorkST commented on June 20, 2024

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.

aminalaee avatar aminalaee commented on June 20, 2024

@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.

StorkST avatar StorkST commented on June 20, 2024

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.

xnuinside avatar xnuinside commented on June 20, 2024

@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.

aminalaee avatar aminalaee commented on June 20, 2024

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.

CasselKim avatar CasselKim commented on June 20, 2024

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.

aminalaee avatar aminalaee commented on June 20, 2024

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.

CasselKim avatar CasselKim commented on June 20, 2024

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.

aminalaee avatar aminalaee commented on June 20, 2024

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.

CasselKim avatar CasselKim commented on June 20, 2024

Agreed with you. Thank you for your kindness :)

from sqladmin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.