生成或移除已在Phoenix中使用生成器后的单个字段的方法。

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

How to generate or remove single field after I've already used generator in Phoenix

问题

I used the phx.gen.html generator to create a table and related controller and view data using this command:

我使用phx.gen.html生成器使用以下命令创建了一个表格以及相关的控制器和视图数据:

mix phx.gen.html TestBeds TestBed testbeds name:string version:string note:string developer:string status:string status_action_value:string

I then wrote code based on the generated table. In retrospect I need another field that populates across the codebase including the form field the generator made.

然后,我根据生成的表格编写了代码。回顾起来,我需要另一个字段,它需要在整个代码库中填充,包括生成器创建的表单字段。

I want to add an additional field using the generator so that I don’t need to manually update each code piece throughout the codebase. How do I do that?

我想使用生成器添加一个额外的字段,以便我不需要在整个代码库中手动更新每个代码片段。我应该如何做?

Also, if I want to remove a field using the generator, how can I do that?

另外,如果我想使用生成器删除一个字段,我该如何做?

Thank you.

谢谢。

英文:

I used the phx.gen.html generator to create a table and related controller and view data using this command:

mix phx.gen.html TestBeds TestBed testbeds name:string version:string note:string developer:string status:string status_action_value:string

I then wrote code based on the generated table. In retrospect I need another field that populates across the codebase including the form field the generator made.

I want to add an additional field using the generator so that I don’t need to manually update each code piece throughout the codebase. How do I do that?

Also, if I want to remove a field using the generator, how can I do that?

Thank you.

答案1

得分: 1

Generators themselves are basically one-way doors. But assuming you use a version control system like git, and assuming the inadequate change has not already been deployed or merged, you can follow a procedure something like this:

  1. 运行 mix.ecto.rollback
  2. 使用 Git 提交您的更改至一个分支,或者至少使用 git add . 暂存它们。
  3. 使用添加/删除字段后运行生成器
  4. 由于迁移上的日期将不同,您将得到另一个迁移文件。选择一个并删除您不需要的那一个,通常是较旧的那个。
  5. 使用更改重新运行迁移。在交互提示中,您将收到两个警告,第一个问您是否真的要向现有上下文中添加内容(回答 Y),第二个问您是否要执行“交互式覆盖”。回答 Y。
  6. 未更改的文件不会提示您做任何操作。但对于每个生成的文件与正在生成的文件不同的情况,您将被提示是否要进行覆盖。您可以选择“Y”,除非您知道您在那里进行了足够重要的代码编辑,最好手动进行更改。
  7. 使用 git diff 或类似工具协调更改与您已经拥有的内容。

如果您已经合并/部署了更改,我会建议创建一个新的 change 迁移(如果您喜欢,您可以使用 ecto 生成器),然后手动更新模式、视图和 changeset 函数。

在实际操作中,像这样的更改几乎肯定需要对您的模型模块、测试夹具、视图以及可能还有您的控制器进行更改,重新运行生成器并不会为您节省太多时间,所以我通常只手动执行这种更改。此外,当脚手架视图与我实际考虑部署的内容足够相似时,生成器为我节省的时间也相对较少。在我自己的团队中,有几个人最终根本不使用生成器。

英文:

Generators themselves are basically one-way doors. But assuming you use a version control system like git, and assuming the inadequate change has not already been deployed or merged, you can follow a procedure something like this:

  1. run mix.ecto.rollback
  2. Git commit your changes "so far" in a branch, or at least stage them with git add .
  3. Run the generator with the added/removed fields
  4. Because the dates will be different on your migration, you'll end up with another migration file. Pick a winner. Delete the one you don't need, which will probably be the older one.
  5. Re-run the migration with the change. You'll get two warnings in interactive prompts, the first asking if you really mean to add things to an existing context (answer Y), and the second asking if you want to do an "interactive overwrite." Answer Y.
  6. Unchanged files will not prompt you for anything. But for each generated file that differs from what is being generated, you'll be prompted whether you want to do the overwrite. You can choose "Y" unless you happen to know you made significant enough code edits there that you'd be better off making the change manually.
  7. Reconcile the changes against what you already have with the help of git diff or a similar tool.

If you've already merged/deployed changes, the procedure I'd follow would be to create a new change migration (you can use the ecto generator for that if you like), and manually update the schema, view(s), and changeset functions.

In practice, a change like this is almost certainly going to require changes to your model module, your test fixtures, views, and probably your controller and re-running the generator doesn't buy you much, so I usually just do this sort of change by hand. It's also unusual when the scaffolded views are similar enough to what I would actually consider deploying that the generator saves me much time. In my own team, a few people end up not using the generators at all.

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

发表评论

匿名网友

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

确定