简体   繁体   English

向现有数据库添加新字段

[英]Adding new field to an existing database

Can anybody help me with adding new column fields into the database.任何人都可以帮助我将新的列字段添加到数据库中。 i have a form where i can add new trainings and another form that has a table.我有一个可以添加新培训的表格和另一个有表格的表格。 The problem is the new trainings i will add should save to the database as a new field.问题是我将添加的新培训应该作为新字段保存到数据库中。 And the new field i will add will also be visible to the table.我将添加的新字段也将在表格中可见。

Please i really need your help.请我真的需要你的帮助。

This is a classic mistake that cannot scale. 这是无法扩展的经典错误。 It's a problem with your schema design. 您的架构设计存在问题。

You should not have to update your schema every time you add a new training. 您不必在每次添加新培训时都更新架构。 You need a one-to-many relationship where you add new training by INSERTing a new row. 您需要一对多的关系,通过插入新行来添加新的培训。

Have a parent table for the form with the HTML table and add a child table with a foreign key to the parent for the form with the new training to INSERT. 使用HTML表为表单创建一个父表,并使用INSERT的新培训将带有外键的子表添加到该表单的父表。

Maybe you should consider your application architecture so that you don't have to perform structure changes in your database at runtime. 也许您应该考虑您的应用程序体系结构,以便不必在运行时在数据库中执行结构更改。

If it's unavoidable you could always use "ALTER TABLE" sentences so that you can add new fields, indexes, or whatever. 如果不可避免,则可以始终使用“ ALTER TABLE”语句,以便您可以添加新的字段,索引或其他内容。

也许您可以通过这种方式使用 add_to_class 方法:

YourModel.add_to_class("fieldname",models.CharField(max_length=20,blank=True, null=True))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM