Kitura / Swift-Kuery-ORM

An ORM for Swift, built on Codable
Apache License 2.0
212 stars 30 forks source link

Kuery-ORM (0.1.1) seems to be adding a character to some table names #50

Closed fwgreen closed 6 years ago

fwgreen commented 6 years ago

I have an existing table called availability on PostgreSQL

CREATE TABLE public.availability
(
  id bigint NOT NULL DEFAULT nextval('availability_id_seq'::regclass),
  starting timestamp with time zone,
  ending timestamp with time zone,
  overtime boolean,
  on_shift boolean,
  CONSTRAINT pk_availability PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.availability
  OWNER TO postgres;

Here's my model

struct Availability : Codable {

    var id: Int
    var starting: Date
    var ending: Date
    var onShift: Bool
    var overtime: Bool
}

extension Availability : Model {}

This is the error message from the database server

ERROR:  relation "availabilitys" does not exist at character 15
STATEMENT:  SELECT * FROM Availabilitys

Oddly, it does not do this with all entity names. It would be great if one could specify table and column names, JPA style: let tableName = "whatever_name" is completely ignored.

EnriqueL8 commented 6 years ago

Hey @fwgreen, to specify the table name you would write:

extension Availability: Model {
   static var tableName: String = "public.availability"
}

Currently we do not support the ability to change the column names. We could look to add this feature!

EnriqueL8 commented 6 years ago

Hey @fwgreen , I have realised that for the custom column names you can just use CodingKeys, like so:

extension Availability: Model {
    enum ColumnKeys: String, CodingKey {
      case id, starting, ending, overtime
      case onShift = "on_shift"
   }
}
fwgreen commented 6 years ago

@EnriqueL8 This is a potentially elegant solution, that at the moment doesn't work: I only have one table without a snake-cased column name and it's the only one that Kuery-ORM can retrieve. Everything else triggers an empty 500 error which isn't caught by the logger, but instead is only seen in my REST client.

PS: While tableName got rid of the original SQL error on the database server, there is still the underlying issue of the magical "s" being appended to some names.

EnriqueL8 commented 6 years ago

Hey @fwgreen, the magical "s" is appended by default to the table names to remove is you can specify the tableName String as I mentioned above. If you think that the magical "s" should not be appended the we could think to add a toggle? For the column names, I made a mistake instead of ColumnKeys it is had to be CodingKeys Like so,

enum CodingKeys: String, CodingKey {
    case id, starting, ending, overtime
    case onShift = "on_shift"
}

Let me know if you need anything else

fwgreen commented 6 years ago

Thanks for putting me on the right path!

Beautylivery commented 5 years ago

Hello, i am quite new user to this project however, i believe an s behind every table just randomly need to be fixed, as this is a bug in the code. Right ?

Clearly its fine that struct name = table name but not struct name = table name + s . Especially working with existing tables like me this gives a whole lot of a mess.

The here presented solution is simply a work around on an issue that still seems to exist.

My example: I try to read out from a table: wp_amelia_providers_to_daysoff ... and i can simply not do it. unless i probably apply the work around which i will now try to do.

But i would still looking forward that the magical s bug is fixed. By the way i am using now: Swift Kuery: 3.0.1 Swift ORM: 0.6.0 SwiftKueryMySQL 2.0.2

fwgreen commented 5 years ago

@Beautylivery The problem is this is a deliberate design choice and not a bug. The architects have assumed that all users are English speakers and all English nouns are pluralized by adding an "S". Feel free to open a new issue.