necromorph23

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
‘’’


Showing Posts 1 to 10

derek-zhou

derek-zhou

What database are you using? What does the database’s SQL shell shows?

necromorph23

necromorph23 OP

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

dimitarvp

Is the ID returned when you run the create_new_user function in iex?

necromorph23

necromorph23 OP

Seems to be the case. Here id is returned nil but id does not seem to be nil in my users table.


sbuttgereit

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 than Repo.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 INSERT query 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) no RETURNING clause at the end of the INSERT, 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

necromorph23 OP

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

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

darnahsan

This is a very useful article to know about some unexpected id behaviour

sbuttgereit

sbuttgereit

As @hlx points out, somewhere along the way in your projects you likely have a difference in the autogenerate settings 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 for returning: true in the insert/2 call.

dimitarvp

dimitarvp

Just have this exactly above each schema ... statements in your project and you should be fine:

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "users" do
    # ...
  end

Where Next? Top

Trending in Questions Top

RSP87
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
kszambelanczyk
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
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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
nseaSeb
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews