【SQLAlChemy】常见的数据类型有哪些,Column可选的参数有哪些呢?

常见数据类型与Column参数

常见类型

  1. Integer:整数类型,对应数据库的 int 类型。
  2. Float:浮点数类型,对应数据库的 float 类型。它占用 32 位空间。
  3. Double:双精度浮点数类型,对应数据库的 double 类型,占用 64 位空间。需要注意的是在 SQLAlchemy 中没有 Double 类型。
  4. String:可变字符类型,对应数据库的 varchar 类型。
  5. Boolean:布尔类型,对应数据库的 tinyint 类型。
  6. DECIMAL:定点数类型,主要用于解决浮点数精度问题。在存储与金钱相关的字段时,推荐使用此数据类型。使用时需要传递两个参数,第一个参数表示字段能存储的总位数,第二个参数表示小数点后的位数。
  7. Enum:枚举类型,限制字段值只能为预设的几个值。在 ORM 模型中,使用 Enum 类型表示枚举。也可以用 Python3 内置的 enum 模块定义字段。
  8. Date:日期类型,只能存储年月日。对应数据库的 date 类型。在 Python 代码中,可以使用 datetime.date 来指定。
  9. DateTime:日期时间类型,可以存储年月日时分秒毫秒等。对应数据库的 datetime 类型。在 Python 代码中,可以使用 datetime.datetime 来指定。
  10. Time:时间类型,可以存储时分秒。对应数据库的 time 类型。在 Python 代码中,可以使用 datetime.time 来指定。
  11. Text:长字符串类型,一般可以存储超过 6 万个字符。如果超出这个范围,可以使用 LONGTEXT 类型。对应数据库的 text 类型。
  12. LONGTEXT:超长文本类型,对应数据库的 longtext 类型。

常见的 Column 参数

  1. primary_key:将此字段设置为主键,需要设定值为 True
  2. autoincrement:将此字段设置为自动递增,需设定值为 True
  3. default:为某个字段设置默认值,常用于如发布时间等字段。
  4. nullable:确定某个字段是否可以为空。默认值为 True,意味着该字段可以为空。
  5. unique:确定某个字段的值是否需唯一。默认值为 False
  6. onupdate:在更新数据时会调用这个参数指定的值或函数。初次插入数据时,不会使用 onupdate 的值,而是使用 default 的值。这个参数常用于 update_time 字段(每次数据更新时都需更新该字段的值)。
  7. name:指定 ORM 模型中的某个属性映射到表中的字段名。如果未指定,则会使用该属性的名称作为字段名。如果已指定,则会使用指定的值作为表字段名。该参数也可以作为位置参数,在第一个参数中指定,如:
    title = Column(String(50),name='title',nullable=False)
    或者
    title = Column('my_title',String(50),nullable=False)

样例实现

class News(Base):
    __tablename__ = 'news'
    # id 整数类型,限制主键自增
    id = Column(Integer, primary_key=True, autoincrement=True)
    # 标题 长度为 100 字符类型, 不能为空
    title = Column(String(100), nullable=False)
    # 作者电话 字符类型 唯一
    author_telephone = Column(String(11), unique=True, nullable=False)
    # 是否删除 整型,不能为空, 默认为 0
    is_delete = Column(Integer, nullable=False, default=0)
    # 是否公开 布尔类型, 不能为空, 默认为 True
    is_published = Column(Boolean, nullable=False, default=True)
    # 标题1 枚举类型 常规写法
    tag1 = Column(Enum('python', 'java', 'go', 'javascript'))
    # 标题2 枚举类型 另一种写法
    tag2 = Column(Enum(TagEnum))
    # 时间的三种类型
    created_at = Column(Date, default=datetime.datetime.now)
    updated_at = Column(DateTime, onupdate=datetime.datetime.now, default=datetime.datetime.now)
    published_at = Column(Time)
    # 文本的两种类型
    content1 = Column(Text)
    content2 = Column(LONGTEXT)

生成的数据库表字段如下:


生成的 SQL 语句如下:

create table news
(
    id               int auto_increment
        primary key,
    title            varchar(100)                                      not null,
    author_telephone varchar(11)                                       not null,
    is_delete        int                                               not null,
    is_published     tinyint(1)                                        not null,
    tag1             enum ('python', 'java', 'go', 'javascript')       null,
    tag2             enum ('python', 'java', 'c', 'javascript', 'cpp') null,
    created_at       date                                              null,
    updated_at       datetime                                          null,
    published_at     time                                              null,
    content1         text                                              null,
    content2         longtext                                          null,
    constraint author_telephone
        unique (author_telephone)
);

测试样例

新增一条数据,并更新相关字段值。

# 新建一个会话对象
session = sessionmaker(bind=engine)()

# 创建一个 new 对象
new_demo = News(title='测试new',
                author_telephone='15991367893',
                is_delete=0,
                is_published=True,
                tag1='go',
                tag2=TagEnum.c,
                created_at=datetime.datetime.now(),
                updated_at=datetime.datetime.now(),
                published_at=datetime.time(),
                content1='这是一个测试性内容1',
                content2='这是一个测试性内容2')

# session.add(new_demo)
# session.commit()
# session.close()

# 测试 onupdate 参数
a_new = session.query(News).first()
a_new.title = '这是修改后的标题'
a_new.author_telephone = '15991367894'
# 提交事物
session.commit()
session.close()

注意

我们设置了author_telephone此处有些不足,不能满足正常开发需求,切忌在项目中使用。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值