File tree Expand file tree Collapse file tree 4 files changed +61
-2
lines changed
test/integration/dialects/postgres Expand file tree Collapse file tree 4 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -569,6 +569,13 @@ class QueryGenerator {
569
569
options . where = this . whereQuery ( options . where ) ;
570
570
}
571
571
572
+ if ( options . schema ) {
573
+ tableName = {
574
+ tableName,
575
+ schema : options . schema
576
+ } ;
577
+ }
578
+
572
579
if ( typeof tableName === 'string' ) {
573
580
tableName = this . quoteIdentifiers ( tableName ) ;
574
581
} else {
Original file line number Diff line number Diff line change @@ -514,7 +514,24 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
514
514
}
515
515
516
516
if ( attribute . references ) {
517
- const referencesTable = this . quoteTable ( attribute . references . model ) ;
517
+ let referencesTable = this . quoteTable ( attribute . references . model ) ;
518
+ const tableDetails = this . extractTableDetails ( referencesTable , options ) ;
519
+ let schema ;
520
+
521
+ if ( options . schema ) {
522
+ schema = options . schema ;
523
+ } else if (
524
+ ( ! attribute . references . model || typeof attribute . references . model == 'string' )
525
+ && options . table
526
+ && options . table . schema
527
+ ) {
528
+ schema = options . table . schema ;
529
+ }
530
+
531
+ if ( schema ) {
532
+ referencesTable = schema + tableDetails . delimiter + referencesTable ;
533
+ }
534
+
518
535
let referencesKey ;
519
536
520
537
if ( attribute . references . key ) {
@@ -610,6 +627,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
610
627
const paramList = this . expandFunctionParamList ( params ) ;
611
628
const variableList = options && options . variables ? this . expandFunctionVariableList ( options . variables ) : '' ;
612
629
const expandedOptionsArray = this . expandOptions ( optionsArray ) ;
630
+
613
631
const statement = options && options . force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION' ;
614
632
615
633
return `${ statement } ${ functionName } (${ paramList } ) RETURNS ${ returnType } AS $func$ ${ variableList } BEGIN ${ body } END; $func$ language '${ language } '${ expandedOptionsArray } ;` ;
Original file line number Diff line number Diff line change @@ -1362,7 +1362,8 @@ class Model {
1362
1362
Object . assign ( {
1363
1363
logging : options . logging ,
1364
1364
benchmark : options . benchmark ,
1365
- transaction : options . transaction
1365
+ transaction : options . transaction ,
1366
+ schema : options . schema
1366
1367
} , index ) ,
1367
1368
this . tableName
1368
1369
) ) ;
Original file line number Diff line number Diff line change @@ -140,6 +140,39 @@ if (dialect.match(/^postgres/)) {
140
140
} ) ;
141
141
} ) ;
142
142
} ) ;
143
+ it ( 'defaults to schema provided to sync() for references #11276' , function ( ) {
144
+ const User = this . sequelize . define ( 'UserXYZ' , {
145
+ uid : {
146
+ type : DataTypes . INTEGER ,
147
+ primaryKey : true ,
148
+ autoIncrement : true ,
149
+ allowNull : false
150
+ }
151
+ } ) ,
152
+ Task = this . sequelize . define ( 'TaskXYZ' , {
153
+ } ) ;
154
+
155
+ Task . belongsTo ( User ) ;
156
+
157
+ return Support . dropTestSchemas ( this . sequelize ) . then ( ( ) => {
158
+ return this . sequelize . createSchema ( 'archive' ) ;
159
+ } ) . then ( ( ) => {
160
+ return User . sync ( { force : true , schema : 'archive' } ) ;
161
+ } ) . then ( ( ) => {
162
+ return Task . sync ( { force : true , schema : 'archive' } ) ;
163
+ } ) . then ( ( ) => {
164
+ return User . schema ( 'archive' ) . create ( { } ) ;
165
+ } ) . then ( user => {
166
+ return Task . schema ( 'archive' ) . create ( { } ) . then ( task => {
167
+ return task . setUserXYZ ( user ) . then ( ( ) => {
168
+ return task . getUserXYZ ( { schema : 'archive' } ) ;
169
+ } ) ;
170
+ } ) ;
171
+ } ) . then ( user => {
172
+ expect ( user ) . to . be . ok ;
173
+ return this . sequelize . dropSchema ( 'archive' ) ;
174
+ } ) ;
175
+ } ) ;
143
176
} ) ;
144
177
} ) ;
145
178
} ) ;
You can’t perform that action at this time.
0 commit comments