Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找id_visual在我的表orders中创建一个名为的字段,该字段从 1 开始并从那里自动递增。我可以在我的模型中创建一个方法来做到这一点,但我认为必须有一个更好更简单的方法。有任何想法吗?
id_visual
orders
据我所知,您想要一个基于主 ID 的辅助 ID?身份密钥只能是基于表的,不能依赖于另一个密钥。您必须在代码中执行此操作,然后将其保存到 before_create 的新字段中。执行此操作的最简单方法是针对您要标识的每个订单,根据主键是什么,获取小于或等于您正在使用的订单的所有订单的计数。它是一个简单的查询计算。
这是您的数据库应该在某个级别提供的东西,无论是在事务中还是在其他锁定中。
看看这个问题,了解一些将 postgres 配置为自动增加列的方法:
PostgreSQL 自动增量