Update column ONLY enabled when column is set as NOT NULL #3666
Replies: 3 comments 1 reply
-
Plus to that, when we use a // log schema
...
historyDate: timestamp('history_date', { mode: 'string' })
// .default(sql`CURRENT_TIMESTAMP`) <-- HERE, this exists in db but I can't set the exact schema here
// so it's not reflecting my db in the schema at all
.notNull(), I understand that when we have this default value it always will add the current_timestamp or whatever we put inside the default method. But when we pass something, it should be able to use what I used to pass to that field/column. In this case, I inject the exact timestamp from another table that are acting in transaction. After that it works as intended, but it seems to not reflect my db constraints at all... Because when I instrospect it reflect the schema with the correct constraints, now I have to overwrite, remove or something else to be able to work as expected inside my codebase. |
Beta Was this translation helpful? Give feedback.
-
Another problem is the GENERATE command, in created sql file as I said before not reflect my db because the CONSTRAINT should exist only in backend/codebase for practical purposes(access field inside query), but in db that CONSTRAINT should not exist! CREATE TABLE IF NOT EXISTS "users" (
...
# wrong
"last_login_at" timestamp NOT NULL,
# correct
"last_login_at" timestamp
...
); |
Beta Was this translation helpful? Give feedback.
-
After that, all works on dev and probably will work in production because my DB schema on dev as expected with the divergence on my schema and db, just to achieve the expected behavior. Now, it appears to be a Issue isntead of a empty discussion. So I start E2E test my application using // drizzle-test.config.ts
export default defineConfig({
out: `./drizzle/dev`, // using dev just to test and the migration will be the same...
schema: './src/db/schema/index.ts',
dialect: 'postgresql',
driver: 'pglite',
dbCredentials: {
url: ':memory:',
},
schemaFilter: ['public'],
breakpoints: true,
}); // // added this to my useFactory no nest
const envENVIRONMENT = configService.get<string>('NODE_ENV');
// testing env
if (envENVIRONMENT === 'test') {
const client = new PGlite(':memory:');
return drizzleE2E(client, { schema });
} The connection works, but I need to create another schema. Because now, I'm using Again, I just need to set Same problem when use Am I missing something on schema configuration?! ENVIRONMENT --> test
Listening port 3001
Something went wrong on user creation error: null value in column "last_login_at" of relation "users" violates not-null constraint
at K_.De (~/backend/node_modules/.pnpm/@[email protected]/node_modules/@electric-sql/pg-protocol/src/parser.ts:432:11) |
Beta Was this translation helpful? Give feedback.
-
This behavior is expected?
Because, I'm only enable to set some field like
lastLoginAt
when.notNull()
is setted in schema, but in my case the column should be able to:NULL
: when we insert NEW users.NOT NULL
: when we update EXISTENT(obviously) users.Is there some strategy to that, something that I missed?
The code below works because of
.notNull
onlastLoginAt
When we remove
.notNull
, now I'm not able to update the respective column aboveNow I'm just able to change username or password, proof by intellisense:
Version:
Beta Was this translation helpful? Give feedback.
All reactions