drizzle-team / drizzle-team/drizzle-orm
[DOCS]: Section `Fundamentals/Schema/Advanced` in documentation should warn about adding modifiers to reused columns
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Enhancement hasn't been filed before.
- [x] 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:
```ts
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:
```ts
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:
```js
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:
```ts
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:
```sql
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.
Contributor guide
Assessment
This issue has not been assessed yet.