Table definition has 'any' type

Ssevenwestonroads4/19/2023
I'm defining all of my tables in an index.ts file; but on some tables like visitor_events I get this errors; 'visitor_events' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. On other tables however, I don't have the error and the PgTableWithColumns is correctly infered. I've restarted TS Server and VSCode but with no success. Ok so I've done a bit of research and here's some context. The error seems to be linked to a circular reference - telling me that something's wrong in my schema. I have 2 tables: visitorEvents and synchronisations - with circular reference that I defined in Prisma with specific names I had given for relations (which wasn't a problem in Prisma, I learned that naming relations explicitly does NOT results in a modified SQL schema). Here was my previous schemas
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
firstVisitorEventId: integer("first_visitor_event_id").references(() => visitor_events.id),
lastVisitorEventId: integer("last_visitor_event_id").references(() => visitor_events.id),
});
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
firstVisitorEventId: integer("first_visitor_event_id").references(() => visitor_events.id),
lastVisitorEventId: integer("last_visitor_event_id").references(() => visitor_events.id),
});
So I ended up doing another table to define this relationship like below.
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
});

export const synchronisation_batches = pgTable('synchronisation_batches', {
// ... fields
synchronisationId: integer('synchronisation_id')
.references(() => synchronisations.id)
.notNull(),
startEventId: integer('start_event_id')
.references(() => visitor_events.id)
.notNull(),
endEventId: integer('end_event_id')
.references(() => visitor_events.id)
.notNull(),
});
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
});

export const synchronisation_batches = pgTable('synchronisation_batches', {
// ... fields
synchronisationId: integer('synchronisation_id')
.references(() => synchronisations.id)
.notNull(),
startEventId: integer('start_event_id')
.references(() => visitor_events.id)
.notNull(),
endEventId: integer('end_event_id')
.references(() => visitor_events.id)
.notNull(),
});
The error disappeared. Here are my following up questions; 1. is typescript unable to infer type is expected behaviour ? 2. can I get more specific errors ? 3. more generally, if pgTable unallows circular references, why ? does it have to do with the order on which my tables are defined - circular references making it impossible to put one before the other ? Context: this is an analytics use case where I need to have a synchronisations table to keep track of a given batch of events to process Seems linked to https://discord.com/channels/1043890932593987624/1043890932593987627/1092514104201195521 Now when I go back to my previous implementation but by putting a AnyPgColumn like below - I still have circular reference but no errors
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
firstVisitorEventId: integer('first_event_id').references((): AnyPgColumn => visitor_events.id),
lastVisitorEventId: integer('last_event_id').references((): AnyPgColumn => visitor_events.id),
});
export const visitor_events = pgTable('visitor_events', {
// ... fields
});

export const synchronisations = pgTable('synchronisations', {
// ... fields
firstVisitorEventId: integer('first_event_id').references((): AnyPgColumn => visitor_events.id),
lastVisitorEventId: integer('last_event_id').references((): AnyPgColumn => visitor_events.id),
});
Why is that ? Thank you 🙏
Ttacomanator4/19/2023
This is in the docs:
sisterCityId: integer('sister_city_id').references((): AnyPgColumn => cities.id), // self-referencing foreign key
sisterCityId: integer('sister_city_id').references((): AnyPgColumn => cities.id), // self-referencing foreign key
You need the AnyPgColumn to prevent the error
Ssevenwestonroads4/19/2023
But why ? Is it unsafe to do so ?
Ttacomanator4/19/2023
I'm not familiar with the details of why. You could do it without AnyPgColumn but it would have to be separated out. This way is a lot cleaner. Unsafe is a relative word. It technically would work without it, but you're going to get a TypeScript error if you don't either separate it out or use AnyPgColumn but I would use AnyPgColumn so you have less manual work to do and your definitions are together.
Ssevenwestonroads4/19/2023
I see thank you. I think it's a TypeScript error and not a pgTable error - as it's possible to have a circular reference in PG but not in TypeScript sense making it impossible to do table definitions containing circular reference in TS without having the AnyPgColumn After reading more about it - I'm going to keep a separate table for my special business use case
Bbloberenober4/19/2023
it's not unsafe, the function signature is the same even without the explicit type annotation it just helps TypeScript to understand there's no actual circular type dependency

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
CTE query of hierarchical dataI'd like to drizzle-ize this query: ```sql WITH RECURSIVE Child(n) AS ( VALUES('...id') createInsertSchema wrong type (drizzle-zod 0.3.1, sqlite))After upgrading to 0.3.1, all of the fields changed to ZodTypeAnyHow do I get the values of an insert inside a transaction?I have this transaction where I create a project and add variables and a history of those variables.Raw sql`` quoting issueHi, I doing a work around until Drizzle supports generated columns. I have a manual script that addsNuxt3 type errorHello, is there somebody who used Drizzle in Nuxt3 project? I have problem with inheriting types froSyntax error mysql migration using composite primary key```sql --> statement-breakpoint CREATE TABLE `member_to_address` ( `member_id` varchar(32) NOT Nwhen using planetscale, using the `.$with()` and `.with()` clauses causes errorIs it not possible to use `$with()` and `with()` with planetscale? it gives the following error: `DaZod prototype mismatchEven though the Zod object constructor name created by Drizzle is ZodObject, it is not an instanceofunrecognized_keysGetting the following error when running generate:pg ``` ZodError: [ { "code": "unrecognized_kRaw SQL / Postgres stored generated column in schemaIs it possibile to add raw sql, or more specifically stored generated columns to the schema? Trying I think i have found another bugtypescript complaining when there's no overridingCustom getter/setter for model's propertyHi, are there any plans for custom getters? (like in TypeORM `transformer`)drizzle-zod type inferLooks there is problem in createInsertSchema type inference. 🤔 (+ need drizzle-zod tag in this posWhy is drizzle-zod converting a string to enum?It seems like it is converting mysql text() or varchar to enums<unknown>?how to do ANYneed to do `where 'myvariable' = ANY(mycolumn)`[BUG?] Postgres transactions throwing connection timeouts after a lot of queriesI think there's a syntax error in Postgres transactions ----> see screenshot cc: @bloberenoberUnique Key SchemaI am trying to create an unique key on a table using a custom schema, the generated sql migration fisqliteTable wrapper which modifies fields, with proper typesMore of a TypeScript question, but I'm trying to create a wrapper for sqliteTable to add a set of stHow to count joined table?I'm trying to count rows returned by join for a query, basically the same as this: https://dba.stackConfig in Typescript?Hello, after upgrading drizzle-kit to 0.17.4 my `drizzle.config.ts` file stopped working. Is it now