Skip to content

[DOCS]: Section Fundamentals/Schema/Advanced in documentation should warn about adding modifiers to reused columns #4336

Open
@ScheiBig

Description

@ScheiBig

Enhancement hasn't been filed before.

  • I have verified this enhancement I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Section Fundamentals/Schema/Advanced in docs (https://orm.drizzle.team/docs/sql-schema-declaration#advanced) suggests that you can easily define columns in one place and reuse them in another.

Since "modifiers" modify actual column object, instead of returning new object, I believe it would be helpful to add some sort of warning that would prevent others from having issues due to column definition being modified - same issues like that I struggled with today.


Let's consider an example table, with definition for reusable foreign key to this table:

export const Users = sqliteTable("Users", {
	userId: text("user_id")
		.notNull()
		.primaryKey(),
	email: text()
		.notNull(),
	firstName: text("first_name")
		.notNull(),
	lastName: text("last_name")
		.notNull(),
	/* ... */
})
const userId = text("user_id")
	.notNull()
	.references(() => Users.userId)

if we were to use this reusable column, but add a modifier to it like so:

export const UserPasswords = sqliteTable("User_Passwords", {
	userId: userId.primaryKey(),
	hash: blob({ mode: "buffer" })
	 	.notNull(),
})

this modifies column stored in userId variable - as source code would suggest:

  primaryKey() /* ... */ {
    this.config.primaryKey = true;
    this.config.notNull = true;
    return this /* ... */;
  }

This means that trying to use this column again in other table without expecting that `userId` was transformed into primary key column:
export const Posts = sqliteTable("Posts", {
	postId: int("post_id")
		.primaryKey(),
	userId,
	/* ... */
}

would result in this error:

LibsqlError: SQLITE_ERROR: table "Posts" has more than one primary key

and migration code that contains error source:

CREATE TABLE `Posts` (
        `post_id` integer PRIMARY KEY NOT NULL,
        `user_id` text PRIMARY KEY NOT NULL,
-- ...

I believe that adding a warning about further modification of reused columns, with simple example of wrong usage / resulting error would be helpful and would prevent errors resulting from trying to use practices, that are shown in official docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions