Background There is evidence gastrointestinal (GI) motility may play a role in the development of GI cancers. Weak opioids (codeine and dihydrocodeine) decrease GI motility, but their effect on GI cancer risk has not been assessed. Aim To assess the association between weak opioids and cancers of the GI tract. Methods A series of nested case-control studies was conducted using Scottish general practice records from the Primary Care Clinical Informatics Unit Research database. Oesophageal (n=2,432), gastric (n=1,443), and colorectal cancer (n=8,750) cases, diagnosed between 1999 and 2011, were identified and matched with up to five controls. Weak opioid use was identified from prescribing records. Odds ratios (OR) and 95% confidence intervals (CI) were calculated using conditional logistic regression, adjusting for relevant comorbidities and medication use. Results There was no association between weak opioids and colorectal cancer (adjusted OR=0.96, CI 0.90, 1.02, p=0.15). There was an increased risk of oesophageal (adjusted OR=1.16, CI 1.04, 1.29, p=0.01) and gastric cancer (adjusted OR=1.26, CI 1.10, 1.45, p=0.001). The associations for oesophageal cancer, but not gastric cancer, were attenuated when weak opioid users were compared with users of another analgesic (adjusted OR=1.03 CI 0.86, 1.22, p=0.76 and adjusted OR=1.29 CI 1.02, 1.64, p=0.04 respectively). Conclusion In this large population-based study, there was no consistent evidence of an association between weak opioids and oesophageal or colorectal cancer risk, but a small increased risk of gastric cancer. Further investigation is required to determine whether this association is causal or reflects residual confounding or confounding by indication.