Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lyndon's avatar

Filament User setup error: Id field

I changed the id field in the users table to use ulids...

Schema::create('users', function (Blueprint $table) {
			$table->ulid('id')->primary();

however, when I try to assign user_id as a foreign key on another table, I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`eomd`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) 

Upon checking the query, I noticed the user_id field is getting an integer value of 1 and I'm not sure where that came from.

insert into `contacts` (`first_name`, `last_name`, `other_names`, `country_id`, `state_id`, `address_line_1`, `address_line_2`, `city`, `postal_code`, `require_email_verification`, `require_phone_verification`, `require_id_verification`, `require_security_question_verification`, `id`, `user_id`, `updated_at`, `created_at`) values (John, Doe ?, 83, 54, GS-0299-3293, ?, Ohio, 00111, 0, 0, 0, 0, 01HGE14CXHQGZDYAN14B3MR76N, 1, 2023-11-29 17:13:24, 2023-11-29 17:13:24)

I tried to log in as another user and it keeps logging back in as this same user with an ID of 1. Is there some extra configuration I need to do for the user setup?

0 likes
7 replies
jaseofspades88's avatar

It looks like you have a pre-existing foreign key with the contacts table that you will need to drop also.

lyndon's avatar

@jaseofspades88 this is how my contacts table is defined:

Schema::create('contacts', function (Blueprint $table) {
            $table->ulid('id')->primary();
			$table->foreignUlid('user_id')->constrained()->onDelete('cascade');

there is no predefined foreign key and I just run a fresh migration as well

jaseofspades88's avatar

You literally have a ->constrained() on your migration, @lyndon. The error you have above is telling you you're trying to add a non-existent user_id to the contacts table and because there is a constraint placed on the table, it fails.

lyndon's avatar

@jaseofspades88 Right... ->constrained is intended. however, the app somehow only logs in as the first user in the database no matter what credentials I use to login and returns an id of 1 which is not the id of any user in the table. I am using ulids and not 'integers' as primary key on the users table as shown above. Question is where it's getting the 1 from and why I cannot login as any other user.

jaseofspades88's avatar
Level 51

Changing the primary key of the model is perhaps the culprit, @lyndon. Have you the following in your User model?

public $incrementing = false;

...this tells your model that the primary key isn't going to auto increment. As it's trying to get 1, I can assume that's what it believes is the first in the auto incrementing primary key.

lyndon's avatar

@jaseofspades88 That was exactly it, although I have never had to do this in any laravel project where I adjusted primary keys so I'm going to assume it's specific to filament (it's my first time using filament) because I realise I have to do that on every other model as well. Thanks a lot for the heads up!

Please or to participate in this conversation.