necromorph23
I was working with absinthe for my new side project but i am getting primary key as null when i register the user but when i return all the users with another query i am getting the id as well. I have no idea how to fix this.
This is my Absinthe Schema
defmodule FlickrWeb.Schema do
use Absinthe.Schema
alias FlickrWeb.Resolvers
#import Types
import_types(FlickrWeb.Schema.Types)
## Reddit Part
#Queries
query do
@desc "Just for testing"
field :test , :string do
"Test passed"
end
@desc "All users"
field :allusers, list_of(:user_type) do
resolve(&Resolvers.UserResolver.allUsers/3)
end
end
#Mutations
mutation do
@desc "Register a new User"
field :registeruser , :user_type do
arg(:input , non_null(:register_input_type))
resolve(&Resolvers.UserResolver.register_user/3)
end
end
end
These are my types:-
‘’’
defmodule FlickrWeb.Schema.Types.Usertype do
use Absinthe.Schema.Notation
object :user_type do
field :id, :id
field :first_name, :string
field :last_name, :string
field :email, :string
field :username, :string
end
input_object :user_input_type do
field :first_name , non_null(:string)
field :last_name , non_null(:string)
field :email , non_null(:string)
field :password , non_null(:string)
field :username, non_null(:string)
end
input_object :register_input_type do
field :first_name , non_null(:string)
field :last_name , non_null(:string)
field :email , non_null(:string)
field :password , non_null(:string)
field :username, non_null(:string)
end
end
‘’’
And this is my User Schema
‘’’
defmodule Flickr.Schemas.User do
use Ecto.Schema
import Ecto.Changeset
@type t :: %__MODULE__{
id: Ecto.UUID.t(),
first_name: String.t(),
last_name: String.t(),
password: String.t(),
email: String.t(),
username: String.t(),
avatarUrl: String.t()
}
@primary_key{:id , :binary_id , []}
schema "users" do
field :first_name, :string
field :last_name, :string
field :email, :string
field :username, :string
field :password, :string
field :avatarUrl, :string, default: "https://as2.ftcdn.net/v2/jpg/03/32/59/65/1000_F_332596535_lAdLhf6KzbW6PWXBWeIFTovTii1drkbT.jpg"
timestamps()
end
def insert_changeset(user , attrs) do
user
|> cast(attrs , [:first_name, :last_name , :email , :username , :password , :avatarUrl])
|> validate_required([:first_name, :last_name, :email , :username, :password, :avatarUrl])
|> update_change(:email, &String.downcase(&1))
|> unique_constraint(:email)
|> unique_constraint(:username)
|> validate_format(:email , ~r/@mnit.ac.in/)
|> validate_length(:username, min: 6 , max: 30)
|> validate_length(:password, min: 6 , max: 20)
|> hash_password
end
defp hash_password(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}} -> put_change(changeset, :password, Pbkdf2.hash_pwd_salt(password))
_ -> changeset
end
end
end
‘’’
This is my creating new user Function:-
‘’’
defmodule Flickr.Mutations.User do
import Ecto.Query, warn: false
alias Flickr.Repo
alias Flickr.Schemas.User
def create_new_user(attrs \\ %{}) do
%User{}
|> User.insert_changeset(attrs)
|> Repo.insert()
end
end
‘’’
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming












Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
derek-zhou
What database are you using? What does the database’s SQL shell shows?
necromorph23
I am using postgresql. The Id is generate automatically and it is visible in shell but it is not being returned after the registration query but when I use a seperate query i am getting the id. Not sure what is going wrong.
dimitarvp
Is the ID returned when you run the
create_new_userfunction iniex?necromorph23
Seems to be the case. Here id is returned nil but id does not seem to be nil in my users table.
sbuttgereit
[rewriting my original response since it was originally poorly written]
Looking things over, it looks like your database is generating the id value (which is fine, I do it that way myself). Assuming that is true, try
Repo.insert(returning: true)rather thanRepo.insert().When you insert a value into the database, the database typically doesn’t return anything to the application aside from some simple diagnostics (like how many rows were inserted). If you look at the
INSERTquery in your screenshot, you can see that 1) the insert isn’t providing the id value so that has to be generated in the database and 2) noRETURNINGclause at the end of theINSERT, so only those diagnostic values are being sent back to the application, not any of the inserted data. That returning clause is the only way the application could know about the new id value.necromorph23
Thanks a lot. it’s working now.
I just had a question though, i have used this same method for a couple of different projects as well and it worked and i never face any issues and i don’t know why it didn’t worked for this one.
Do you have any idea what could be the real problem why it was not working earlier?
hlx
How are you generating your id’s, seems that you have disabled the option
autogenerate?e.g.
@primary_key {:id, :binary_id, autogenerate: true}darnahsan
This is a very useful article to know about some unexpected id behaviour
sbuttgereit
As @hlx points out, somewhere along the way in your projects you likely have a difference in the
autogeneratesettings in this project vs. your other projects.In your other projects where you’re not
returning: true, you may still have database table definitions that default the id to a generated UUID/(other type) value, but those are only defaults. If Ecto is configured to itself generate the id value, the database will never try generate its own default value since you’ve provided a value through the application… and Ecto will have that id value already, obviating the need forreturning: truein theinsert/2call.dimitarvp
Just have this exactly above each
schema ...statements in your project and you should be fine: