如何在SQLalchemy中创建列时声明datetime范围?

huangapple go评论62阅读模式
英文:

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(&#39;salary &gt; 0 AND salary &lt; 100&#39;))
  hire_date =db.Column(DateTimeRange(&quot;2020-01-01 00:00:00&quot;, 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 = (&#39;id&#39;, &#39;name&#39;, &#39;department&#39;, &#39;salary&#39;, &#39;hire_date&#39;)

# 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(&#39;hire_date =&gt; &quot;2020-01-01 00:00:00&quot; AND hire_date &lt;= current_datetime&#39;)) 

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'
)
)


`=&gt;` 是一个错误;对于当前时间,也许对于 sqlite,您应该使用 `datetime('now', 'localtime')`。
英文:

For the time constraint on the hire_date, I'd suggest:

__table_args__ = (
  CheckConstraint(&quot;hire_date BETWEEN &#39;2020-01-01 00:00:00&#39; AND CURRENT_TIMESTAMP&quot;),
    )

After your third update 如何在SQLalchemy中创建列时声明datetime范围?: you should use:

db.Column(
  db.DateTime, db.CheckConstraint(
    &#39;hire_date &gt;= &quot;2020-01-01 00:00:00&quot; AND hire_date &lt;= current_timestamp&#39;
  )
)

=&gt; is an error; for the current time, maybe for sqlite you should use datetime(&#39;now&#39;, &#39;localtime&#39;).

huangapple
  • 本文由 发表于 2023年5月8日 02:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定