-
Notifications
You must be signed in to change notification settings - Fork 64
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
Custom Primary Key support #96
Comments
Great idea putting this all in one spot 👍 |
It would be really nice to have the class CreateUsers::V20190610161436 < Avram::Migrator::Migration::V1
def migrate
create :users, :none do
add id : Int64, primary: true
end
end
def rollback
drop :users
end
end Mainly I want it to not automatically generate id's, but still allow the creation of a primary key without using a custom SQL query. |
Adding in my own personal use cases: I have an app that needed to migrate data from an old database in mysql with int32, to a new postgres app. We had to differentiate between the two by using Some of the join tables in this rails app have no primary key at all. So the current setup throws an error when trying to fetch an So supporting custom String, and no id are both options I would like to see. |
Also, I want to make it so the primary key type is WAY more extendable. I think the primary key should be just like any other column but be included in the generator. In other words, no default primary key or timestamps, instead add the defaults in the generated code. |
I actually love that idea @paulcsmith. Doing this would just about support the whole list! create :users do
primary_key id : Int64
add name : String
end |
I love that idea too. Having a default is a very ActiveRecord thing. It's nice for vanilla db setups, but makes you dig if you want to do anything extra. |
@watzon Yeah I like having a default so you have something to get going with, but like you said, it is hard to customize. Doing it in code with the generators still gives us a nice default, but also makes things discoverable. I think it should work well! |
Ok so I think there are a couple potential issues with this approach Potential issues
Solving itI like the idea of making this explicit instead of having a million config options, but I don't love typing the same stuff in every model. So what if we add a macro to the generated abstract class BaseModel < Avram::Model
macro default_columns
primary_key id : UUID
timestamps # sets up created_at/updated_at and callbacks to set them on create/update
end
end Then in the generators (or if people type by hand) class User < BaseModel
table do
default_columns
# Other columns
end
end This is shorter, easier to remember, easy to customize. Thoughts? cc @edwardloveall @HarrisonB |
First thing is, did you leave off the I'd be ok with this solution too. I have a project using UUID, and it's already a pain redefining |
Actually you can leave off the table name right now. But of course it is not documented :) Glad you like it. I was thinking the same. It is more flexible. Still easy to use and not too much typing. At least that’s my hope haha |
Also I think it’s good we don’t document that yet until we have some kind of table name inference in migrations too. Like |
Wait a second.. You can leave the table name out? I was definitely going to be suggesting that, so that's great news! |
I like this. While reading through I through about a bunch of different ways this could be less than ideal.
I think what Paul came up with has the right tradeoffs. It's easy to remember (most people probably won't even know what |
Excellent points @edwardloveall. I think you're right and I think what I proposed might be something that people can still forget so I think we can add just one more step to make this extra easy, even when not using generators. Maybe when calling the |
That's a pretty good compromise. Plus we will document this to make sure it's easy to understand. |
Does a97c2b7 or a previous commit allow for composite keys (see original post of this issue)? If not, this issue shouldn't be closed yet. |
No it doesn't. I'll open separate issues for those other items so we don't have one big one that is hard to track. Thanks for calling that out |
#129 this will require a lot more work since it will require accepting multiple values for a lot of methods. Update, find, delete, and lots of internal methods so might take longer to finish |
No worries, just wanted to have an issue to track. |
Ref: https://gitter.im/luckyframework/Lobby?at=5cfedb153dcdab400324037a
Related:
#6
#21
#87
#89
The current support for primary keys in Avram is limited to
UUID
UUID andSERIAL
Int32. Other options requested are:id SERIAL
)id UUID
)id BIGSERIAL
) Customize defaults and primary key #121String (or something like (KSUID) Feature request: Add support for prefixed KSUID primary_key_type #21id character varying(18) NOT NULL
)Composite (Add support for composite primary keys #129PRIMARY KEY (id_1, id_2)
)Int16 (Add Int16 small serial primary key #130id SMALLSERIAL
)id
field.)Since several people request this, and there's already quite a few issues open about it, I wanted to put them all in to 1 location to track. If you have a need for a custom primary key, let us know what your use case is, and if the current support is blocking or not for you. This helps us to prioritize and/or gauge what is wanted.
The text was updated successfully, but these errors were encountered: