acrolink
I have a special need to set the id of newly created entities to some integer relating to UNIX timestamp (not autoincrement based). Any idea how to do this? i am using Phoenix 1.3. Thank you.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
Are you asking about ecto or elixir’s structs?
With structs you can add something like a
newfunction to return them somewhat prepopulatedAnd you can use
@primary_keyattribute to configure the schema’s primary key.acrolink
I have tried your code but it fails.
defstruct ..gives an errordefstruct has already been called for Entre.Warehouse.Item, defstruct can only be called once per moduleand if I omit that line and keep thedef new(name) doblock I getEntre.Warehouse.Item.__struct__/1 is undefined, cannot expand struct Entre.Warehouse.Item.I am using Phoenix 1.3. I am not sure if the errors I get are related to the newly introduced contexts.
idi527
Sorry, I’ve probably confused you …
The example with
defstructthat I’ve provided seems to not be what you want. I just wondered if you were asking about ecto or elixir structs.Judging by the error you get, you were asking about ecto.
To get custom ids you should use
@primary_key.tomconroy
Try doing it with a Postgres function, like this
http://rob.conery.io/2014/05/28/a-better-id-generator-for-postgresql/
voger
@idi527: Sorry, I accidentally hit reply in your post. This reply was meant for the original post.
If your question is about how to do custom primary keys with Ecto then here is a guide in phoenix blog
http://phoenixframework.org/blog/custom-primary-key
acrolink
I am going to use the
idfield itself but instead of having auto-generated serial values I want to set Unix timestamps asids. I am usingEcto. I understand that the place to set the value is inside the model definition file but still cannot figure out the way to do that. ThePostgressolution can do the job, but I am interested in knowing how to do that in Phoenix. Any help is highly appreciated. Thank you.voger
In the blog post that I linked it says how to edit the migration
If I am not mistaken there is nothing stopping you from naming your primary key “id”. I haven’t tried naming a custom primary key “id”. You have to experiment a bit with this :).
Also if you look in the
add/3documentation you will see it can take the option:defaultSo you can use that to apply the Postgres function.
acrolink
@voger
I am just looking at the whole thing from a RoR background. There I could just do
Here it looks like the more professional way to do this is inside the schema definition and possibly by PostgreSQL itself. My little question in the meantime,
self.id = nand that’s allfragment/1uses a PostgreSQL function/procedure or can it consume a value returned from anElixirfunction?voger
Fragment runs SQL code/functions as prepared statements. You can use it if you want to use a custom Postgresql function to handle your ID generation inside the database. Here is an example about using fragments in Ecto: Fragments in Ecto – Learning Elixir
If you wan’t to handle ID generation with an Elixir function you don’t need a fragment. You can use put_change/3 in your changeset in your schema when you create it.
Another option is, if you want to make your code cleaner, you can use a custom ecto type as your field and define an autogenerate/0 function. This way you will still be generating your code with Elixir but you will be able to use that type of field in different structs. Here is an example of using uuids for primary key https://blog.fourk.io/uuids-as-primary-keys-in-phoenix-with-ecto-and-elixir-1dd79e1ecc2e. This example looks a bit like what you are trying to achieve. Also here is the code for the UUID type. Notice how it defines an
autogenerate/0function that just callsgenerate/0.Disclaimer: I haven’t actually had the chance to use a custom field.
acrolink
@voger
Thank you for the valuable information. I have tried to use the
put_change/3insidedef changeset(%Item{} = item, attrs) do. It works sort of. It sets the value at the time the form is rendered, not when the entity is saved to the database. In my situation, the value is time related so it should reflect the time when an entity is saved.I guess, first and 3rd solutions you described can help me achieve the desired result. I have considered setting the value inside the create function. I am not sure if that’s a proper way to do it. I am not sure even how that can be done.
UPDATE
I have just realized that setting the
idin the controller is not possible, it must be done either in DB, by custom data-type as suggested by @voger or by some other model way.