处理 South 中的 PostgreSQL 串行字段类型
我正在使用一个遗留数据库,它做了一些以数据库方式有意义的事情,但不确定如何在 Django 中表示它们,以便 South 和 Django 本身可以处理它们。
我有一个以 PartCode 为键的零件表 我有一个以 VendorCode 作为键的供应商表
我有一个 PartsVendor 表,其中包含零件和供应商的 FK,以及有关关系的其他信息。我使用的是“through”参数,因此它是独立的,但它使用 PartCode+VendorCode 作为复合键,这是 Django 中不支持的。仅当使用 South 或像 dumpdata 这样的函数想要查看主键时,我才会遇到问题。然而,这些都是相当大的问题。
我的临时解决方案是仅添加一个 _id 字段作为 AutoField 并在 Postgres 中添加一个串行字段,效果很好,但是当使用 South 时,它会因 default=False 而 NOT NULL 为 true 的事实而窒息。
我已经尝试编写自定义字段,但这似乎是一个死胡同,因为我实际上并没有更改有关字段类型的任何内容。
I am using a legacy database which does a couple things that make sense in a db way, but not sure how to represent them in Django so that South and Django itself can deal with them.
I have a Parts table with PartCode as the key
I have a Vendor table with VendorCode as the key
I have a PartsVendor table with FK's to Parts and Vendor, as well as additional information about the relationship. I am using the "through" parameter so it stands on it's own, but it uses the PartCode+VendorCode as a composite key, something not supported in Django. Only when using South or functions like dumpdata where it wants to see a primary key do I run into an issue. However, those are pretty big issues.
My temporary solution was to just add an _id field as AutoField and added a serial field in Postgres which works fine, but then when using South it chokes on the fact that is default=False and NOT NULL is true.
I've gone down the path of trying to write a custom field, but this seemed like a dead end since I am not actually changing anything about the field type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PostgreSQL 中,串行类型实际上并不是常规类型。
Serial 的作用是用序列中下一个数字的默认值设置一个整数字段。该序列存储在数据库中的其他位置(也可以手动创建)
我没有对此进行测试,但似乎逻辑序列字段都将在 Django 中表示为整数。应用 默认值 属性添加到字段并将其保留在插入内容中。
我希望这有帮助。 :)
In PostgreSQL, serial type isn't actually a regular type.
What serial does is set an integer field up with the default value of the next number in a sequence. The sequence is stored elsewhere in the database (and can be manually created as well)
I've not tested this, but it would seem logical serial fields would all be represented as Integers in Django. Apply the default attribute to the Field and leave it off your inserts.
I hope that helps. :)