Ruby on Rails-是否可以在我创建新桌子时添加服装列?
每当我为用户创建表格时,它总是具有ID列。我希望每当我创建一个新表时,就始终像ID一样添加Codum_column(在我的情况下)。
Whenever I create a table for User, it always have a ID column. I want that whenever I create a new table, a costum_column (uuid, in my case) always be added as a column, just like the ID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(假设您在轨道上谈论Ruby)
如果您希望ID成为UUID,则最简单的是,
如果您希望所有create_table都可以添加另一个自定义列,例如public_id,那么您可以将猴子补丁添加到您的create_table方法
此解决方案IMO的问题是您正在隐藏事物。使用该方法的人(合作者或同事,尤其是更多的初级同事)并不期待它,并且会从
IMO出现的意外行为来搜索一个更好的选择是创建一个助理,该助手仍然可以手动地称呼它,但是一行或使用简单的方法调用。所以没有什么隐藏的,但这很容易。
那你就可以
(assuming you are talking about ruby on rails)
if you want the id to be a uuid the easiest is to have the following
If you want all create_table to ALWAYS add another custom column, such as public_id, then you could add a monkey patch to your create_table method
The issue with this solution IMO is that you are hiding things. People (collaborators or colleagues, especially more junior ones) who use the method are not expecting it and will search hours from where this unexpected behavior come from
IMO a better alternative is to create a helper that would allow to still manually have to call it, but in one line, or with a simple method call. so nothing is hidden, but it's easy.
then you can just