英文:
How to declare a range in the datetime while creating a column in SQLalchemy?
问题
I have a table named "Employee", where I have a requirement to set the column value of "hire date" to a datetime object in the format of 'YYYY-MM-DD HH:MM:SS', with a range from 01-01-2020 00:00:00 to today.
class Employee(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
department = db.Column(db.String(50))
salary = db.Column(db.Float, db.CheckConstraint('salary > 0 AND salary < 100'))
hire_date = db.Column(DateTimeRange("2020-01-01 00:00:00", datetime.date.today()))
def init(self, name, department, salary, hire_date):
self.name = name
self.department = department
self.salary = salary
self.hire_date = hire_date
Employee Schema
class EmployeeSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'department', 'salary', 'hire_date')
Init schema
employee_schema = EmployeeSchema(strict=True)
employees_schema = EmployeeSchema(many=True)
So when I am trying to create the table in sqlite, I'm getting this error:
in _init_items raise exc.ArgumentError( sqlalchemy.exc.ArgumentError: 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected, got 2020-01-01T00:00:00 - 2023-05-07T00:00:00
Can anyone help me with this problem? How do I define the constraint?
Update:
I tried putting the current datetime in a variable to do something like this:
current_datetime = datetime.date.today()
hire_date = db.Column(db.DateTime, db.CheckConstraint('hire_date >= "2020-01-01 00:00:00" AND hire_date <= ?', current_datetime))
Which is giving me this error now:
sqlite3.OperationalError: near ">": syntax error
英文:
I have a table named "Employee", where I have a requirement to set the column value of "hire date" to a datetime object in the format of 'YYYY-MM-DD HH:MM:SS', with a range from 01-01-2020 00:00:00 to today. I did the following:
class Employee(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
department = db.Column(db.String(50))
salary = db.Column(db.Float, db.CheckConstraint('salary > 0 AND salary < 100'))
hire_date =db.Column(DateTimeRange("2020-01-01 00:00:00", datetime.date.today()))
def __init__(self, name, department, salary, hire_date):
self.name = name
self.department = department
self.salary = salary
self.hire_date = hire_date
# Employee Schema
class EmployeeSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'department', 'salary', 'hire_date')
# Init schema
employee_schema = EmployeeSchema(strict = True)
employees_schema = EmployeeSchema(many=True)
So when I am trying to create the table in sqlite, I'm getting this error:
> in _init_items raise exc.ArgumentError( sqlalchemy.exc.ArgumentError:
> 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected,
> got 2020-01-01T00:00:00 - 2023-05-07T00:00:00
Can anyone help me with this problem? How do I define the constraint?
Update:
I tried putting the current datetime in a variable to do something like this:
current_datetime = datetime.date.today()
hire_date =db.Column(db.DateTime, db.CheckConstraint('hire_date => "2020-01-01 00:00:00" AND hire_date <= current_datetime'))
Which is giving me this error now:
> sqlite3.OperationalError: near ">": syntax error
答案1
得分: 1
对于`hire_date`上的时间限制,我建议使用以下代码:
table_args = (
CheckConstraint("hire_date BETWEEN '2020-01-01 00:00:00' AND CURRENT_TIMESTAMP"),
)
**在您的第三次更新 :)**:您应该使用以下代码:
db.Column(
db.DateTime, db.CheckConstraint(
'hire_date >= "2020-01-01 00:00:00" AND hire_date <= current_timestamp'
)
)
`=>` 是一个错误;对于当前时间,也许对于 sqlite,您应该使用 `datetime('now', 'localtime')`。
英文:
For the time constraint on the hire_date
, I'd suggest:
__table_args__ = (
CheckConstraint("hire_date BETWEEN '2020-01-01 00:00:00' AND CURRENT_TIMESTAMP"),
)
After your third update : you should use:
db.Column(
db.DateTime, db.CheckConstraint(
'hire_date >= "2020-01-01 00:00:00" AND hire_date <= current_timestamp'
)
)
=>
is an error; for the current time, maybe for sqlite you should use datetime('now', 'localtime')
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论