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

Custom table name? #164

Closed
bobobo1618 opened this issue Apr 2, 2016 · 3 comments
Closed

Custom table name? #164

bobobo1618 opened this issue Apr 2, 2016 · 3 comments

Comments

@bobobo1618
Copy link

Is it possible to create a model but specify a custom table name? If so, how does one do so?

@vmihailenco
Copy link
Member

Yes, it is possible:

type MyStruct struct {
    TableName struct{} `sql:"custom_table_name"`

    Id int
    ...
}

I need to add an example.

@bobobo1618
Copy link
Author

Thanks! Is it possible to specify the table name directly at query time? There are cases like sharding where one may have multiple tables with the same schema (model) but different names.

@vmihailenco
Copy link
Member

I have slightly different approach implemented in https://github.com/go-pg/sharding using named params, e.g.

type User struct {
    TableName string `sql:"?shard.users"`

    Id        int64
    AccountId int64
    Name      string
    Emails    []string
}

then in Shard wrapper I set value for ?shard variable - https://github.com/go-pg/sharding/blob/v4/shard.go#L29-L30

That way you can use named param in all queries (without ORM), e.g.

cluster.Shard(accountId).DB.Query("SELECT * FROM ?shard.table WHERE shard_id = ?shard_id")

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