Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to manually sepecify column as Primary column option #244

Closed
lafriks opened this issue Nov 3, 2021 · 5 comments · Fixed by #247
Closed

Allow to manually sepecify column as Primary column option #244

lafriks opened this issue Nov 3, 2021 · 5 comments · Fixed by #247
Labels
enhancement New feature or request

Comments

@lafriks
Copy link
Contributor

lafriks commented Nov 3, 2021

Currently there is no way to create BigID or ID column and not specifying it as primary key (it will always be created as primary key) but there are cases where you need to combine id and additional column as primary key - this is needed for partitioned tables in PostgreSQL

@Fs02
Copy link
Member

Fs02 commented Nov 4, 2021

can you share what the final API will looks like?
I'm also curious on why not use normal fields and then PrimaryKeys?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 4, 2021

Primary problem is not that much about being able to define BigID or ID columns (that ar BIGSERIAL or SERIAL in postgresql) and set them not being primary key. For example with my proposed API you can define something like:

This would be needed to define partitioned table in PostgreSQL:

schema.CreateTable("audit", func(t *rel.Table) {
  t.BigID("id", rel.Primary(false))
  t.DateTime("timestamp", rel.Required(true))
  // other fields
  t.PrimaryKeys([]string{"id", "timestamp"})
  t.Options = `PARTITION BY RANGE ("timestamp")`
 })

or you could define other type column as primary with alt syntax:

schema.CreateTable("client", func(t *rel.Table) {
  t.String("id", rel.Primary(true), rel.Limit(64))
  // other fields
})

that would be equivalent to using:

schema.CreateTable("client", func(t *rel.Table) {
  t.String("id", rel.Limit(64))
  // other fields
  t.PrimaryKey("id")
})

@Fs02
Copy link
Member

Fs02 commented Nov 4, 2021

as enhancements of above case I'm thinking to implicitly set Primary to false on column if t.PrimaryKeys is called 🤔
so your example can be shortened to:

schema.CreateTable("audit", func(t *rel.Table) {
  t.BigID("id") // no need to specify Primary(false), will automatically set by `t.PrimaryKeys`
  t.DateTime("timestamp", rel.Required(true))
  // other fields
  t.PrimaryKeys([]string{"id", "timestamp"})
  t.Options = `PARTITION BY RANGE ("timestamp")`
 })

what do you think?

@Fs02
Copy link
Member

Fs02 commented Nov 5, 2021

@lafriks can you update above prs?
I've tagged rel@v0.29.0 and sql@v0.6.0

@lafriks
Copy link
Contributor Author

lafriks commented Nov 5, 2021

updated, can be merged, closing issue :)

@lafriks lafriks closed this as completed Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants