Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key with a column name that is not "id" #890

Open
theunreal opened this issue Apr 15, 2022 · 1 comment
Open

Foreign key with a column name that is not "id" #890

theunreal opened this issue Apr 15, 2022 · 1 comment

Comments

@theunreal
Copy link

I have the following expression inside createTable:

item_code: {
            type: 'varchar',
            notNull: true,
            references: '"items"',
            onDelete: 'cascade',
        },

Currently, it doesn't work because it references to items.id instead of items.item_code, after going over all docs - couldn't see any way to sett the name of the target column.

Am I missing something or this is a missing feature?

@mike-usa
Copy link

mike-usa commented Apr 28, 2022

Am I missing something or this is a missing feature?

You can do this by overriding the references value string:

item_code: {
  type: 'varchar',
  notNull: true,
  references: 'items(item_code)',
  onDelete: 'cascade',
}

Note: items.item_code must be a constraint on your items table and share the same datatype; in your case a varchar.


Ordinarily, you could also pass a Name object to references:

references: {
  schema: 'public',
  name: 'items'
}

I was perusing the TypeScript code to see if it would accept any other keys, but it doesn't. It looks like expanding the Name object to include column (string)/columns (comma-delimited string or array of strings) may be worthwhile. Then, in a later breaking change, the name key should be renamed to table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants