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

Not being able to create a FK for a table created in another database schema #701

Closed
PedroStrabeli opened this issue Nov 27, 2019 · 4 comments

Comments

@PedroStrabeli
Copy link

PedroStrabeli commented Nov 27, 2019

So I'm trying to create table for authentication on my postgresql db.

This table should be in a dedicated schema in my database, called, let's say: authentication, to be simple. All's good, I can define and create the table by using:

object UserTable: Table("authentication.user") {
  val id = integer("id").primaryKey().autoIncrement()
  val name = varchar("name", 200)
  val type = integer("type").references(UserTypeTable().id)
}

The creation of the table is ok on a different schema without the FK, but when it defines the FK, it fails, because it uses the name of the table as suffix for the FK's name.

So it's something like fk_authentication.user_fk_type_id, which is a no go, since there cannot be a . on the fk's name and I get a syntax error.

[main] WARN Exposed - Transaction attempt #0 failed: org.postgresql.util.PSQLException: ERROR: syntax error at or near "."
  Statement(s): CREATE TABLE IF NOT EXISTS authentication.user (id INT PRIMARY KEY, "name" VARCHAR(200) NOT NULL, type INT NOT NULL CONSTRAINT fk_authentication.user_fk_type_id FOREIGN KEY (type) REFERENCES authentication.user_type(id) ON DELETE RESTRICT ON UPDATE RESTRICT)
org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: syntax error at or near "."

note: I changed the statement to be relatable to the query I wrot:e here as an example. Do not attain to the statement, but to the problem at hand, to correct the syntax.

So I think of 2 possibilities:

  • Define table schema in another way, somehow?
  • Define FK name explicitely.

Can I do any of those? or should I approach this in another way?
I'm trying to do both by looking at the Table.kt code, but not finding anything that I can use... Is there a workaround?

Thank you

@Tapac
Copy link
Contributor

Tapac commented Nov 29, 2019

Duplicate of #145

@Tapac Tapac marked this as a duplicate of #145 Nov 29, 2019
@PedroStrabeli
Copy link
Author

PedroStrabeli commented Nov 30, 2019

Sorry, but I beg to differ. This is not a duplicate. At max it's an extension
#145 goes on about creating table and joining cross-schema. I was able to create the table in a different schema, as stated on my question, but the issue here is creating a Foreign Key on that table, in another schema.
As stated, the automatic naming of the Foreign Key is the problem. The automatic naming uses fk_[name_of_table]fk[field1]_[field2].

And the issue is: If the name of the table was set with a schema on it, the name of the fk gets a dot (.) on it, which invariably fails the syntax. A FK name cannot contain a dot.

This is the problem.
The solution I'm looking for is either:

  1. Name the FK myself
  2. Set the schema differently for the table (Which from PostgreSQL: Referencing data on another schema #145 I believe is not possible).

Thank you

Tapac added a commit that referenced this issue Dec 1, 2019
Tapac added a commit that referenced this issue Dec 1, 2019
@Tapac
Copy link
Contributor

Tapac commented Dec 29, 2019

Should be already released

@Tapac Tapac closed this as completed Dec 29, 2019
@PedroStrabeli
Copy link
Author

Great, thanks.

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